- Home /
Collider Confusion
I have a script that allows an item score to increase when passing through each of the three main colliders. like so
public int items = 0;
void OnTriggerEnter(Collider Enter) {
if (Enter.CompareTag ("Item1")) {
items += 1;
}
if (Enter.CompareTag ("Item2")) {
items += 1;
}
if (Enter.CompareTag ("Item3")) {
items += 1;
}
}
And what I am trying to do is to create a system out of it. You cannot get Item2 and Item3 without Item1 first. And Item3 without both Item1 + Item2. Going crazy on figuring out the most efficient way. Any Help would be great.
Answer by Apples_mmmmmmmm · Dec 06, 2012 at 04:11 AM
public int items = 0;
private bool haveItem1 = false;
private bool haveItem2 = false;
void OnTriggerEnter(Collider Enter)
{
if (Enter.CompareTag ("Item1"))
{
items += 1;
haveItem1 = true;
}
else if (Enter.CompareTag ("Item2") && haveItem1)
{
items += 1;
haveItem2 = true;
}
else if (Enter.CompareTag ("Item3") && haveItem1 && haveItem2)
{
items += 1;
}
}
Thanks a lot bro. It works for the most part but, when I sit in Item1 collider, it constantly increases count. How would I make that same script but allow it to only count once? Thanks again jack.
Essentially the way that OnTriggerEnter() works is that every frame the game is running it will check for collision and if the check returns true will execute the code inside OnTriggerEnter().
If you want to limit the items from being increased, then you can use an if statement to check if the itemCount is below a value, and if it is then it will execute the line for add items.
You could also cause the object tagged "Item1" to be destroyed after the call for items ++; in order to make it unable to pick up the item a second time.
please TIC$$anonymous$$ any useful answers, to close out the question. cheers
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer
C# collider problem with tags/GameObject.names 1 Answer
Multiple Cars not working 1 Answer
Can't get a laser working properly. 2 Answers