Trying to remove an object at time of collision C#

So, I am attempting to remove a game object when it collides with one of my enemies.

At the moment, I have the object (which is a spell) being removed from the scene after 2 seconds, but am having a difficult time checking for collisions with my enemy to remove the spell at the time of contact.

The problem I’m having is that when calling the OnCollisionEnter method, I am unable to reference the “GameObject spell” I instantiated in the update method, and I don’t know of a way to check for a collision inside of the update method, which is what I would like to do.

My current code is as follows:

void Update () 
	{
		cooldownTimer++;//Increment

		if(Input.GetButton("Spell_1") && cooldownTimer >= 50)
		{
			GameObject spell = Instantiate(prefab) as GameObject;

			Destroy(spell.gameObject, 2);//Destroy Spell

			spell.transform.position = transform.position + transform.forward;//Shoot forward
			spell.transform.position = transform.position + transform.up * 1.35f;//Correct the position of our spell so it appears to come from chest level

			Rigidbody rb = spell.GetComponent<Rigidbody>();
			rb.velocity = transform.forward * 100;
			cooldownTimer = 0;//Reset cooldown timer
		}
	}

	void OnCollisionEnter(Collision collision) 
	{
		if(collision.gameObject.tag == "Enemy")
		{
			Destroy(spell.gameObject);//Destroy Spell
			Debug.Log("Delete Spell");
		}
	} 

What would be the best way to go about removing the spell at the time of contact, is there a way to check for the collision inside of the update method, so I could use an if/ else statement to either remove at the time of contact or after 2 seconds?

Any pointers appreciated.

Hello,

2 things that could help you:

  1. Your cooldown timer is not a timer. Depending on how fast the machine is running your spell cooldown will change. You can add Time.deltaTime in a variable and check when the sum is over your cooldown for example.
  2. Your code to make the spell disappear (both with a timer and on collision) should be in a script attached to the spell itself. Then you’ll have no difficulty referencing the spell as it will be the gameobject itself.

dont use Destroy when collide with enemy , use SetActive(false) , because after 2 seconds spell(gameobject) will be destroy auto by your update destroy method.

void OnCollisionEnter(Collision collision) 
     {
         if(collision.gameObject.tag == "Enemy")
         {
           //  Destroy(spell.gameObject);//Destroy Spell
            spell.SetActive(false);
             Debug.Log("Hit Player");
         }
     }

The problem will be that, OnCollisionEnter checks for collision between the gameobject it is attached to and something else, but from the look of your code this code is not attached to you spell object so it will not be called.

You should add you OnCollisionEnter code to a script which is attached to the spell prefab you are creating.

For future reference, to refer to the spell variable in any method Update(), OnCollisionEnter() Custom()… you need to create it as a global variable, outside of any method

Class SomeClassDefinition{
    GameObject spell;

    void Update(){
        spell = .....
    }

}

You should try like this:

 GameObject spell;

 void Update () 
 {
     cooldownTimer += Time.deltaTime;
     if(Input.GetButton("Spell_1") && cooldownTimer >= 50)
     {
         spell = Instantiate(prefab) as GameObject;

         Destroy(spell.gameObject, 2);//Destroy Spell

         spell.transform.position = transform.position + transform.forward;//Shoot forward
         spell.transform.position = transform.position + transform.up * 1.35f;//Correct the position of our spell so it appears to come from chest level

         Rigidbody rb = spell.GetComponent<Rigidbody>();
         rb.velocity = transform.forward * 100;
         cooldownTimer = 0;//Reset cooldown timer
     }
 }

 void OnCollisionEnter(Collision collision) 
 {
     if(collision.gameObject.tag == "Enemy")
     {
         Destroy(spell.gameObject);//Destroy Spell
         Debug.Log("Delete Spell");
     }
 }