I started to feel like i knew what i was doing. I managed to get objects to move and react to each other, then this collision problem came along and stumped me! Any help would be greatly appreciated. My scene has a cannon which instantiates a sphere with a rigidbody and sphere collider attached. This sphere is used as a projectile. The scene also contains some targets, which are simple cubes with box colliders attached. The idea is for the projectile to bounce off everything until it hits a target, then other things should happen, but at the moment i just want to destroy the sphere instance. I've been through all the documentation and threads on the forum and have come up with something i think should work. Please can someone explain what i'm doing wrong. I have the following script attached to the target: Code (csharp): var thingThatHits : GameObject; function onCollisionEnter(other){ if(other == thingThatHits) { Destroy(thingThatHits); } } I then drag the original projectile object onto thingThatHits in the inspector. This is sort of based on a piece of code i found in a thread, but that code had the equivalent of me writing: Code (csharp): var thingThatHits : projectile; But when i try this unity says projectile is not a valid type. Thanks for any help!
After a colon you indicate the type of the variable. Using GameObject should be ok. 'projectile' with a lowercase 'p' is probably not a type (e.g. if your script is called Projectile, the type is Projectile with an uppercase P)
Sorry, i forgot to put in my original post, i've tried using the script which includes the thingThatHits : GameObject. But the projectile still bounces off the target.
Did you write OnCollisionEnter with an uppercase 'O' (in your script it's lowercase)? You can check whether the function is called at all by doing something like Code (csharp): print("I'm called!"); in there.
I'm pretty sure i typed OnCollisionEnter, unfortunately i'm at work at the moment so don't have access to unity, but i copied the syntax from the documentation so i am assuming i used uppercase. Is there any reason that you can see why my script isn't working? Or can you think of a better way to do the same job?
Comparing objects can be a tricky. By default comparing objects is done by checking if the two variables point to the same object instance. (Except for value types like Vector3, int, float and string where the value is compared instead.) You are comparing a Collision (other) to a GameObject (thingThatHits.) This can never be the same object. Try this instead (I changed so you compare the transform property of the Collision to a stored transform reference): Code (csharp): var thingThatHits : GameObject; function OnCollisionEnter(other){ if(other.transform == thingThatHits.transform) { Destroy(thingThatHits); } }