- Home /
How to do Multiple OnTriggerEnters
I've seen a couple of old posts that ask this question but i'm having trouble understanding the answers people have given. Can anyone explain it to me in an easy to understand way?
here is the code i'm trying to use. I need to return which tile the player is standing on (In this case the grasslandTile) and change the true/false for the other tiles.
void OnTriggerEnter(Collider grasslandTile){
enterGrassland = true;
enterHouseWithTree = false;
enterSingleTree = false;
}
But I also need one thats like
void OnTriggerEnter(Collider houseWithTreeTile){
enterGrassland = false;
enterHouseWithTree = true;
enterSingleTree = false;
}
and then
void OnTriggerEnter(Collider singleTreeTile){
enterGrassland = false;
enterHouseWithTree = false;
enterSingleTree = true;
}
So how do I get these all into one script? I need them all in one script to return the value for this
void OnGUI(){
if(guiOn && enterGrassland == true)
{ specific
GUI.Button(new Rect(10,200,1200,100), "Forage");
GUI.Button(new Rect(10,400,1200,100), "Rest");
GUI.Button(new Rect(10,600,1200,100), "Wait");
}
if(guiOn && enterHouseWithTree== true)
{ specific
GUI.Button(new Rect(10,200,1200,100), "Search");
GUI.Button(new Rect(10,400,1200,100), "Rest");
GUI.Button(new Rect(10,600,1200,100), "Wait");
}
}
}
Answer by GambinoInd · Feb 26, 2014 at 06:06 AM
There can only be one OnTriggerEnter and it will be called no matter what name the collider has or what you name the collider variable in the method. What you need to do is within the method, check for the gameobject's name.
For example:
void OnTriggerEnter(Collider collider){
if(collider.gameObject.name == "Tree Tile") {
enterGrassland = false;
enterHouseWithTree = false;
enterSingleTree = true;
} else if(collider.gameObject.name == "Grass Tile") {
//Do stuff.
}
}
You can put them all in one script, by simply putting them all in one script! I don't quite understand the issue with that part.
I see thank you that was much easier to understand. So something like this?
void OnTriggerEnter(Collider collider){
if(collider.gameObject.name == "grassLandTile")
{
enterGrassland = true;
enterHouseWithTree = false;
enterSingleTree = false;
}
else if(collider.gameObject.name == "houseWithTreeTile")
{
enterGrassland = false;
enterHouseWithTree = true;
enterSingleTree = false;
}
else(collider.gameObject.name == "singleTreeTile")
{
enterGrassland = false;
enterHouseWithTree = false;
enterSingleTree = true;
}
}
Yep that should work. Just don't forget to add the if to the last else if in the code you posted.
After a few typo fixes got it working perfectly ;) thanks for making it so easy to understand!
Answer by justinpatterson · Feb 26, 2014 at 06:12 AM
Basically what you want to do is
(1) add tags to or give your objects individual names that the player can stand on
(2) In your code have a switch statement that looks at the tag/name of the thing it collided with.
void OnTriggerEnter(Collider singleTreeTile){
switch(singleTreeTile.gameObject.name){ // or singleTreeTile.gameObject.tag
case "grassland":
// true false false
break;
case "houseWithTree":
// false true false
break;
case "houseWithTree":
//false false true
break;
}
}
Tags are another option, good idea justin. I recommend using switches as well, I just didn't use them in my answer to keep it simple to understand.
In this specific example, with only three cases, an if else chain would be faster by single digit milliseconds. It's only at about 5 or more that you actually start seeing better efficiency. Or at least that's what I've read.
That's true, Aethonosity. I prefer switches for readability, but the primary point of the post was to emphasize tags over names for the sake of control. I just hate typing out if statements and needing to write "==" a thousand times and whatnot heh. The switch statement, while I admit emboldening it was a bit much, was the secondary point.
We're basically at a point in game dev where time saved by using a switch statement are insignificant in comparison to just good program$$anonymous$$g practices elsewhere. I know this post is pretty dang old, but I figured I'd add my two cents to your comment. Thanks for inviting a discussion on the differences between ifs and switches for new programmers finding this post.
That all makes a lot of sense. I appreciate your response! I'm a pretty new programmer, so I'm definitely still learning where it's important to optimize and where it's just an unnecessary headache. Haha