- Home /
Destroy object on collision and update script.
Ok so yet another simple javascript question. When i collide with a coin, the coin gets removed and the GUI is updated displaying the score.
Well thats what i wish for, there are no errors and nothing is happening.
@ CollisionScript
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.name == "Coin"){
ScoreScript.score ++;
Destroy(collision.gameObject,0);
}
}
@ ScoreScript
static var score : float = 0;
function Update(){
guiText.text = "Score : " + score;
}
Appreciate any feedback to what the problem is.
~Wentzel
is your gameObject named Coin, or Coin(clone) ? You can check what is happening. Use the below script and see what is printed in the console window when you hit a collider :
function OnCollisionEnter(collision : Collision){
Debug.Log( "Collision! collision.gameObject.name = " + collision.gameObject.name);
if(collision.gameObject.name == "Coin"){
ScoreScript.score ++;
Destroy(collision.gameObject,0);
}
}
Wentzel : @ alucardj Thanks for the quick reply.
Unfortunatly stil not doing anything or showing any thing on the console. I Exported the single object from max to .Fbx imported it in to unity and just draged one onto the screen.
So i have no idea what the problem could be.
Any ideas ?
If you are getting no output in the console, the collider Object is not set up properly for OnCollisionEnter.
Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
does your object have a rigidbody component?
Answer by Tyanext · Jul 02, 2012 at 08:48 PM
You sure you have attached a rigidbody to one of the collision objects
Despite of being a simple issue, I spent quite much of time reading solutions/troubleshootings, before co$$anonymous$$g here. So here's my comment: Although in Unity Script Reference is stated in a smart/short way "OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.", it was easy enough for me to pass by the meaning of the relationships: collider<->rigidbody or rigidbody<->collider.
Also, by skim$$anonymous$$g the text at first, I translated this: "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." to this: "IF the collider has a rigidbody attached, it must be a non-kinematic one.", in my $$anonymous$$d. All in all, pislar's answer has cleared my above confusion that also might derives from my very low experience, of course.
Btw here everything is explained thoroughly.
Answer by Wentzel · Jul 02, 2012 at 07:38 PM
@ alucardj Thanks for the quick reply.
Unfortunatly stil not doing anything or showing any thing on the console. I Exported the single object from max to .Fbx imported it in to unity and just draged one onto the screen.
So i have no idea what the problem could be.
Any ideas ?
Your answer
Follow this Question
Related Questions
collision script to destroy 2 Answers
Please help with a simple collision. 3 Answers
Destroy on Collision? 1 Answer