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;
}
}
}