Hi! I have a player object and an Item object. My player object has a script that should delete my item object upon collision. My item object has a script with an id number. I want to use that ID number to identify my Item object. Here is the code for the player that handles the collision: Code (CSharp): private void OnCollisionEnter(Collision collision) { if (collision.gameObject.GetComponent<Item>().id == 999) { Debug.Log("HIT"); } } When running this i get this error in the console: I don't really understand. Any help appreciated! Thanks in advance!
the null ref tells me it does not find the Item script. put the collision detection on the item it's self. and check to see if the thing that hit the item is the player Code was free handed, sorry for any typo's Code (CSharp): private void OnCollisionEnter(Collision collision) { if (collision.transform.gameobject.tag == "Player") { Debug.Log("HIT by the Player"); } }
Another option/consideration in general, is to make sure you have the correct game object : Code (csharp): Item theItem = collision.gameObject.GetComponent<Item>(); if(theItem != null) { // now we know it's there / was on the game object with which we had a collision. }