- Home /
 
OnCollisionEnter Stopped Working
Okay, so I had code working for a Snake game in which, like the classic, you pick up an object, the score goes up and if you hit your tail or a wall it's Game Over. But for some reason now the food does not destroy itself when picked up. I know I am hitting the food as the score goes up but the food doesn't disappear then reappear.
Here is my collision code:
 var player : GameObject;
 var food : GameObject[];
 var Tail : GameObject;
 var lastTail : GameObject;
 
 
 
 
 function Start () {
 
 }
 
 function Update () {
 
 }
     
     function OnCollisionEnter (collision : Collision)
     {
         
         if(collision.transform == player)
             
     {
         
             addTail ();
             Destroy(gameObject);
         
     
     var foodname    = Instantiate(food[(Random.Range(0,food.Length))],
          new Vector3(Random.Range(-48, 96), 1.5, 
          Random.Range(-23, 22)), 
          Quaternion.identity) as GameObject;
          
          foodname.name = "Food";
         
     }
     }
     
     function addTail ()
     {
     var newTail : GameObject = Instantiate(Tail, transform.position, Quaternion.identity);
     newTail.name = "Tail";
     newTail.GetComponent.<SmoothFollow>().target = lastTail.transform;
     lastTail = newTail;
     }
 
              where is the score being added from? i dont see it in this script
The score is in a separate script:
 #pragma strict
 
 var points = 0;
 var scoreBox : Texture;
 var style : GUIStyle;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnCollisionEnter(collision : Collision)
 {
 if(collision.transform.name == ("Food"))
 {
 points += 1;
 }
 }
 
 function OnGUI()
 {
 var pointsName = points.ToString();
 
 GUI.Label(Rect(20,20,150,75), scoreBox);
 GUI.Label(Rect(175,30,150,75), pointsName, style);
 }
 
 
                 Answer by MojtabaM · Jul 14, 2013 at 08:27 PM
first script belongs to which object ? player ?
  function OnCollisionEnter (collision : Collision)
     {
       if(collision.transform == player)
          {
               addTail ();
               Destroy(gameObject);
              var foodname   = Instantiate(food[(Random.Range(0,food.Length))],
              new Vector3(Random.Range(-48, 96), 1.5, 
              Random.Range(-23, 22)), 
              Quaternion.identity) as GameObject;
              foodname.name = "Food";
          }
     }
 
               as i know , you said here "just when player hit itself, destroy itself" . i don't see food destroy cod in your script !!!
i think you should change :
  if(collision.transform == player)
 
               to this :
  if(collision.gameObject.name == "Food") 
     Destroy(collision.gameObject);
 
               Just if this script attached to Player.
so , use tags to detect player . and did your code work before this ?
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to destroy bullet on collision! 2 Answers
Instantiate with Prefabs 2 Answers
Destroy on collision 3 Answers