- Home /
OnCollisionEnter for other gameobjects (C#)
What i want to code (C#) is a trigger that activates, for example a timer, with an OnTriggerEnter. However i want to stop the same timer with an OnTriggerEnter function for an other gameobject. What i think (and hope) it whould look like:
public gameobject _Start; //(gameobject or Rigidbody etc?)
public gameobject _End;
private bool thebool;
void _Start.OnTriggerEnter(Collider otherobject)
{
thebool = true;
}
void _End.OnTriggerEnter(Collider otherobject2)
{
thebool = false;
}
This however, does not work so my question is: 'How whould I make this work?' I would apreciate any help and hope someone could help me.
@aWolf$$anonymous$$ing you can add a boolean to your code. The boolean could be used as a flag (or a semaphor) to say whether the timer is activated by another object. So, check in OnTriggerEnter of object "_End": if OnTriggerEnter of object "_Start" has activated the flag then stop the timer in _Start. You can do it vise versa.
@aida12_99 Thanks, I think this will work as an alternative. I wanted to use 1 script for this, but I guess it is not possible so I will use this;
Answer by Ali-hatem · Apr 11, 2016 at 04:20 PM
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "object1"){
// do something
}
if(other.gameObject.name == "object2"){
// do something
}
}
you can replace gameObject.name with gameObject.tag if you deal with many objects that you want something to happen when one of them entered the area but you have tag them in inspector first .
Answer by aWolfKing · Apr 11, 2016 at 05:57 PM
@Ali hatem Sorry, i think you misunderstood me; What i meant is: OnTriggerEnter only applies to the gameobject the script is placed in. I want to know how i would check if somthing collides with gameobject a if the script is in gameobject b. I appreciate your help, but this was not what i meant (I am not very skilled in explaining, sorry).
in gameobject a you can cheek if the collided object is gameobject b & vice versa gameobject a script :
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "objectB"){
// do something
}
}
but if still not what you want please explain what exactly you want to do like if Enemy collide with Player & something should happen so the picture will be clear .
@Ali hatem Sorry, still wat i meant. If you put an script with OnTriggerEnter in it, that void will 'start' when something collides with the gameobject the script is in. However, what I want is an OnTriggerEnter void that 'starts' when something collides with an other gameobject than the gameobject is in. I hope I made clear what I want and thank you for you help.
1- it's the same you don't need to put a script to Game Object you want to deal with if it collide with Game Object have a script with OnTriggerEnter for example :
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
// other means the Game Object has collided with this Game Object.
Destroy(other.gameObject);
}
}
2- but if you mean OnTriggerEnter should be called in object because of other 2 objects collided with each other it's impossible if you want to detect collisions you have to put OnTriggerEnter in object & then you can apply any thing to the object that have the OnTriggerEnter founction or to the other object that collided with it but you can put a bool to be true when they collide if you want to detect there collisions in a third object.
3- lastly back to your main question if you want object 1 to activate timer when collide with something & object 2 deactivate timer when something collide with it then you should have OnTriggerEnter in both object 1 & object 2 :
// object 1 script
void OnTriggerEnter(Collider other){
// activate timer
}
// object 2 script
void OnTriggerEnter(Collider other){
// deactivate timer
}
@Ali hatem The 3rd one is what i meant, I was wondering if that was possible in just 1 script. Your and the answer aida gave me answered my question, it is not possible. Thanks for your help and I will use multiple scripts to achieve what I wanted to code.
it's possible if you place OnTriggerEnter in the object that collide with the object 1 & object 2 by testing there name or tag & activate or deactivate depend on that so look at my answer & consider it's on the object that collide with the object 1 & object 2 .
Your answer
Follow this Question
Related Questions
How to attach two colliders (i.e two or more game objects )? 1 Answer
Issue With Input.GetKey and IENumerator in Grabbing Script 1 Answer
How to find all gameobjects that use the same material and change it? 1 Answer
How to generate objects from a script in the editor, i.e. before runtime? 1 Answer
Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers