- Home /
Detect Collision in an If Statement?
Basically, what I'm doing is this.
If the player presses a button: If a certain object (already made a variable) is colliding with another object (also a variable) add 1 to points. Else start game over phase.
Any ideas how this can be implemented? Specifically the colliding part as everything else I already have implemented...
Answer by maddFrogg · Apr 10, 2014 at 11:34 AM
Try this:
void OnCollisionStay(Collision collisionInfo) {
if ( Input.GetMouseButtonDown(0) {
if (collisionInfo.gameObject.name == nameofYourVariable) {
// Do stuff
}
}
}
This method should be in a class associated to the object NOT called nameofYourVariable
Note: Both objects must have a Collider and a Rigidbody associated.
Thanks for the response but will this work if none of the two objects has this script attatched to it?
The OnCollisionStay $$anonymous$$ethod will only be called if it's in a script that exists on the colliding object. However, you can just do stuff like this:
//this class is on the colliding gameobject
void OnCollisionStay(Collision collisionInfo)
{
Collision$$anonymous$$anager.Singleton.HandleCollision(gameObject, collisionInfo);
}
And the other class would look something like this, and can be anywhere in the scene.
public static Collision$$anonymous$$anager Singleton;
void Start
{
Singleton = this;
}
public void HandleCollision(GameObject source, Collision collisionInfo)
{
//do stuff with the info
}
Important note: In this case, the Collision$$anonymous$$anager should exist only once in your entire scene at all times.
Thank you for all the replies guys! I decided to go with the original script and move the Game script which controls everything to the object since it wouldn't make a difference to how the game plays haha.
The thing is, nothing happens. I did everything correctly and what not but when I click nothing happens. I made an else statement to restart the level if they weren't colliding to test and nothing still happens. Any ideas?
void OnCollisionStay2D (Collision2D collisionInfo){
if(Input.Get$$anonymous$$ouseButtonDown(0)){
if(collisionInfo.gameObject == line){
score = score + 1;
print ("it worked!");
}
else{
Application.LoadLevel("level");
}
}
}
thanks for the help!
Please check if both the gameobject with this script and the gameobject being hit both have a Collider2D attached and one of them has a rigidbody2D. They must not be triggers, and the rigidbody must not be kinematic. If you dont want any collision but still want them to react to each other, turn both colliders into triggers and use OnTriggerStay2D() ins$$anonymous$$d.
arrghh the issue with that is that they have to be kinematic :/ ill try using ontriggerstay ins$$anonymous$$d. thanks for the tip! :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trying to animate on collision, Help Please. 0 Answers
How to call OnTriggerEnter once 5 Answers
how to go through platform like bananakong? 1 Answer
Scene Change Collision 2 Answers