0
votes

So im having trouble with referencing a object in play not a prefab Ver simple code that will blow up a car when the player hits hit. The sphere collider gets turned on to do damage when it blows up

So what happens is when there are 2 or more cars in the playfield, If the player hits one of them , the first one put into play will blow up then any further attempt to hit them hill lock up as the gameobject does not exist anymore. I know the problem is referencing the object in play not the prefab but how do i do that

2 codes on the object that blows up

public class ObjectExplode : MonoBehaviour
{
    // This goes on objects that will be destroyed that hurt tags.
    [SerializeField] AudioSource explosionSound;
        [SerializeField] GameObject explosionPrefab;// Set the explosion particle 
    [HideInInspector] public bool explode = false;// make the object explode

    private SphereCollider sphereCollider;

    void Start()
    {
        sphereCollider = GetComponent<SphereCollider>();
        sphereCollider.enabled = false;
    }

    public void Explode()
    {
        sphereCollider.enabled = true;
        GameObject explosionClone = (GameObject)Instantiate
            (explosionPrefab, gameObject.transform.position, gameObject.transform.rotation);//as GameObject;
        Destroy(gameObject, 2);
        explosionSound.Play();
    }

}

Here is the second script

public class ObjectDamage : MonoBehaviour {

    [SerializeField] int startingHealth = 1000;// starting health of the car
    private int currentHealth;// current health of car
    //Class and componant assigns
    private ObjectExplode objectExplode;


    void Start()
    {
        currentHealth = startingHealth;// start  the level with ful health

        //Class and componant setups
        objectExplode = FindObjectOfType<ObjectExplode>();
    }

    void Death()
    {
        gameObject.SetActive(false);// destroy the car 
    }

    public void Hurt(int damageToTake)
    {
        currentHealth -= damageToTake;
        if (currentHealth <= 0)
        {
            Death();
            objectExplode.Explode();
        }
    }
}

Corrected code for those interested

public class ObjectDamage : MonoBehaviour {

    [SerializeField] int startingHealth = 1000;// starting health of the object
    private int currentHealth;// current health of object
    private ObjectExplode objectExplode;

    void Start()
    {
        currentHealth = startingHealth;// start  the level with ful health
        objectExplode = gameObject.GetComponent<ObjectExplode>();

    }

    void Death()
    {
       gameObject.SetActive(false);
    }

    public void Hurt(int damageToTake)
    {
        currentHealth -= damageToTake;
        if (currentHealth <= 0)
        {
            Death();
           objectExplode.Explode();
        }
    }
}

And second code

public class ObjectExplode : MonoBehaviour
{
    // This goes on objects that will be destroyed 
    [SerializeField] AudioSource explosionSound;
    [SerializeField] GameObject explosionPrefab;// Set the explosion particle 
    [HideInInspector] public bool explode = false;// make the object explode

    private SphereCollider sphereCollider;

    void Start()
    {
        sphereCollider = GetComponent<SphereCollider>();
        sphereCollider.enabled = false;
    }

    public void Explode()
    {
        sphereCollider.enabled = true;
        GameObject explosionClone = Instantiate(explosionPrefab, gameObject.GetComponent<Rigidbody>().transform.position,gameObject.GetComponent<Rigidbody>().transform.rotation);
        Destroy(explosionClone, 2);
        explosionSound.Play();
    }
}

And add this code to the character you want to do the damage

public class DamageToGive : MonoBehaviour
{
    private int damage = 0;
    public int damageToGive;
    public string getTag = "";
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == getTag && damage == 0)
        {    
            if (getTag == "TAGGOESHERE")
            {
                other.GetComponent<ZombieHealth>().Hurt(damageToGive);
            }
            damage++;
        }
    }
    Void OnTriggerExit(Collider other)
    {
        if(damage > 0)
        { 
            damage = 0;
        }
    }
}
1
Added fixed code for Reference - Melsy

1 Answers

1
votes

Well of course it's going to blow up the first one you placed into the scene. That's what FindObjectOfType() does. It finds the first object in the scene hierarchy that has the type specified.

Description:

Returns the first active loaded object of Type type.

You want to blow up a specific car you need to get a reference to that specific car.

It looks like your second script is attached to the same object that the first script is attached to, so what you actually are looking for is gameObject.GetComponent<ObjectExplode>().