- Home /
Network.Destroy in OnCollisionEnter; not working.
I'm in the process of learning Unity's network features (so please excuse any horrendous code I paste), and I'm having trouble with a Network.Destroy inside an OnCollisionEnter.
Current function is as follows:
function OnCollisionEnter(collision : Collision)
{
if (networkView.isMine && collision.collider.gameObject == target)
{
Network.Destroy(gameObject);
Debug.Log("Destroyed");
}
}
The log won't even work, so I'm guessing it's to do with the if statement. This script is obviously attached to the object I want to destroy, while the 'target' (set through the inspector as a variable) is the other object. Both objects have rigidbodies and colliders, and neither are set as triggers. They are both also prefabs, if that makes any difference. Thanks for any help.
First what is target?
Second you should do some Debug logging like this:
function OnCollisionEnter(collision : Collision)
{
Debug.Log("CollisionEnter gets called");
Debug.Log("Is NetworkView $$anonymous$$e? " + networkView.is$$anonymous$$ine);
if(collision.collider.gameobject == target)
Debug.Log("Collision GO is the same as target");
else
Debug.Log("Collision GO and target are different");
if (networkView.is$$anonymous$$ine && collision.collider.gameObject == target)
{
Debug.Log("We nearly did it :)");
Network.Destroy(gameObject);
Debug.Log("Destroyed");
}
}
This way you can check where the problem may be.
Ok, so I added the debugging lines and it reaches the "Collision GO and target are different" line (networkView.is$$anonymous$$ine returned as true). Guessing it's to do with the collision comparison then?
'target' is a GameObject variable in the script, which is set in the inspector as a prefab bullet object; the object that this is supposed to collide with.
I converted my comment to an answer so that you can accept it to mark the question as solved.
Answer by ExTheSea · May 05, 2013 at 06:24 PM
The Prefab is not the same as the instance which is colliding with this gameobject. You should do your gameobject detection with tags. Just add a tag to the bullet like "Bullet" and then you can do:
if (networkView.isMine && collision.collider.gameObject.tag == "Bullet")