- Home /
How do you detect if two specific objects are touching.
I am currently making a game in which if the spikes touch the player the player gets destroyed. I looked around other questions and it seemed that people were using code like this:
function Update () {
function (OnCollisionStay (collision:Collision)){
if (collision.collider.tag == "Player"){
Destroy(collision.gameObject);
};
};
};
I was going to apply this script to all the objects that destroy the player. Can I get some help please? I'm very new to this so forgive me if this is incredibly simple.
In UnityScript, you can't define a function in a function. The OnCollisionStay is a function defined outside of the Update function
ok i changed it should this work
#pragma strict
function OnCollisionStay (collision:Collision){
if (collision.collider.tag == "Player"){
Destroy(collision.gameObject);
};
};
function Start () {
}
function Update () {
}
#pragma strict
function OnCollisionStay(collision:Collision){
if (collision.collider.tag == "Player"){
Destroy(collision.gameObject);
}
}
just get rid of the semicolons, otherwise looks good
Well that has to be some other part of your code, the example given by Seth is correct. Double-click on the error message in unity, it should open the script and highlight the offending line. $$anonymous$$ake sure all your opening brackets have closing brackets (in the right place), and check your syntax/punctuation, as your previous comments show you are making mistakes there too.
please note that the error gives you the EXACT LOCATION of the problem:
in this picture for example, it would be in the script called $$anonymous$$oveTurn, line 22, the 6th character which is causing the issue...
Answer by Prabir · Mar 21, 2013 at 07:59 AM
function (OnCollisionStay (collision:Collision)) { if (collision.collider.tag == "Player") {
Destroy(collision.gameObject);
}
}
This Should Work, if not you can check the exact line where is the error. Its seems a simple error.
Please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
BTW this is incorrect. Please correct your errors and format your code before you receive negative votes.
Your answer
Follow this Question
Related Questions
Collision not working 1 Answer
How to add rigidbody with script? 1 Answer
Adding sound javascript in unity2d 1 Answer
Object Collision PLEASE HELP!!! 1 Answer