c# collide and destory won't work.Please check what went wrong?

Good day!

I am making a game in which the runner, when it collides with another prefab object which is entitled “foodpowgen1”, the food is completely destroyed and the player earns a score.

I have attached the “destroy code” to the the runner object as follows:

using UnityEngine;
using System.Collections;

public class runner : MonoBehaviour {

	
public Vector2 jumpForce = new Vector2(0, 300);


	void  OnCollisionEnter ( Collision collision  ){ 
		if (collision.gameObject.name == "foodpowgen1"){ // if the hit object's name is foodpowgen1...
			Destroy(collision.gameObject); // destroy it
		}
	}


	// Use this for initialization
	void Start () {
	
	
	}

	// Update is called once per frame
	void Update () {

		// Jump
		if (Input.GetKeyUp("space"))
		{
			rigidbody2D.velocity = Vector2.zero;
			rigidbody2D.AddForce(jumpForce);
		}
		// Die by being off screen
		Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
		if (screenPosition.y > Screen.height || screenPosition.y < 0)
		{
			Die();
		}
	}
	
	// Die by collision
	void OnCollisionEnter2D(Collision2D other)
	{
		Die();
	}
	
	void Die()
	{
		Application.LoadLevel(Application.loadedLevel);


			
	}
}




The next code is the code of the foodpwgen1:



using UnityEngine;
using System.Collections;

public class foodpowgen1 : MonoBehaviour {

	public GameObject sans;
	int score = 0;



	// Use this for initialization
	void Start()
	{
		InvokeRepeating("CreateObstacle", 1f, 1.5f);
	}
	

	// Update is called once per frame
	void OnGUI () 
	{
		GUI.color = Color.black;
		GUILayout.Label(" Score: " + score.ToString());
	}

	
	void CreateObstacle()
	{
		Instantiate(sans);
		score++;
	}
}

As you can see from the code, when runner collides with the prefab foodpowgen1, the player earns a score. The problem is, I can’t get the runner to completely destroy foodpowgen1. How do I go about this?

any response is appreciated.

i don’t know whether I am understanding your problem correctly.but if your OnColliderEnter Method is not working then check whether the runner has a rigidbody attached to it and also if the rigidbody is attached then check that IsKinematic property is unchecked. bcoz collision events occur only when one of the colliding body has a non kinematic rigidbody attached to it