- Home /
how to detect if one object is touching another
I want to change levels..So everytime that the player touches the EndLevel point it will start another level..
this is what i've come to so far
function OnCollision(col : Collision) {
if (col.gameObject == "Player" ) {
Application.LoadLevel (1);
}
}
But it doesn't seem to work , i get no errors , yet nothing is happening in the actual game.
"i get no errors": ins$$anonymous$$d of giving errors, javaScript will often just skip the line, or guess what you might have meant.
$$anonymous$$issing ".name" would give a "can't compare these" error in C#. Oddly, missing the ENTER after OnCollision isn't technically an error, so you always have to double-check those.
Answer by Alzander2001 · Mar 27, 2015 at 03:16 PM
This is what i used for the next level portal:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
DontDestroyOnLoad(other.gameObject);
Application.LoadLevel("ENTER LEVELNAME HERE");
}
}
This is C# and sorry about that. You must also tag the player as "Player". What this is doing is running this function whenever anything touches it, and if the tag is "Player" it will load the selected level.
P.S If you have a 3D game, just Change "OnTriggerEnter2D" to "OnTriggerEnter", and change "Collision2D" To "Collision".
Thanks! -Alec
Answer by Wrymn · Mar 22, 2013 at 10:18 PM
You need to write it like this:
function OnCollisionEnter(col : Collision) {
if (col.collider.name == "Player" ) {
Application.LoadLevel(1);
}
}
or if you want to use it with TAG:
function OnCollisionEnter(col : Collision) {
if (col.collider.tag == "Player" ) {
Application.LoadLevel(1);
}
}
I know it's been a while since this topic was active, but I hope someone will see my question:
When you write: col.collider.name == "Player" Witch name do you write? The gameObject's name?
Answer by BPR · Mar 22, 2013 at 10:07 PM
Hi, you are comparing a gameObject and a string, try to use col.gameObject.name or col.gameObject.tag instead
Regards, BPR
Answer by AcidDreamer · Mar 30, 2013 at 10:41 AM
Thanks both for your answers.. It seems I cant set the unity correctly to behave according to the script...What I basicly did was adding this script to the a cube i wanted to use as a winpoint.. But when the player touches it , nothing happens
Your answer
Follow this Question
Related Questions
how to get objects that are in some radius with the point in certain center 1 Answer
How to reach multiple GameObjects' value , enemy AI 1 Answer
Is it possible to detect console prints? 1 Answer
gameObject A detecting keyboard inputs from gameObject B. 1 Answer
detect 360 spin on mobile device 1 Answer