So I wrote a little script to tell me where my character is. When my character triggers with a cube, the location is decided.
Location2 is inside Location1 ( For example House>Kitchen). So this is what I did:
If I run the game and it works. it works for the entire run. But sometimes (3/4 times), it doesn’t work for the entire run, and I have to run it again for it to work. Am I missing something? Or is this some sort of bug?
(When it doesn’t work, it never switches to Location2, but stays at Location1).
EDIT: I still don’t know what the issue is, I went with another strategy that is much messier and primitive, but at least it works, still interested in finding out what happened there.
docs says OnTriggerStay fires almost all the frames while conditions met…
well, you are asking it to check for One, set a bool , and then check for Two
so… i dont know what youre scene is like
maybe in one frame it has the bool for One but then it never fires again for Two ???
This is the wrong sub forum for script questions. Please read the description of the sub forum:
You are using OnTriggerStay, which is called continiously while you are inside the triggerzone. Since you are inside Zone 1 AND Zone 2, this function is called for the object named “one” AND the object named “two”. Depending on which object is handled last by physics you end up with Location being equal to either “Location1” or “Location2”. You don’t have any guarantee for which is handled first and you end up with non-deterministic behaviour. You should fix this by using OnTriggerEnter.
Other things to consider:
Do you need to require the player to be inside the trigger named “one” before changing the location? This makes sense if there is a spot where the two triggers does not overlap and you only want to set Location to “Location2” at the spot where they overlap. If this isn’t the case, do like Nanity says, but don’t use OnTriggerStay for the reason explained above.
Are you resetting the boolean InOne? If you are not, you won’t get the behaviour you expect. - set it to false when OnTriggerExit is called for the object named “one”.
if(myBoolean == true) is the same as if(myBoolean) it doesn’t make sense to compare it to true.
I never have issues with triggers or anything. What I do at least in C# is
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "Whatever"){
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
do something
}
}
(I put a bunch of Do Somethings) because I can make the statements that big and it will always run that big).
Meaning, it's the way you are calling everything. Try different ways and it will work.
And like someone else said, this is the wrong forum for this type of question.