Hey i have a script which starts and stops the timer in another script.
The problem is that when i leave the OnTriggerExits´ collider the first time it immediately think that i left it twice so it jumps from round 1 to 3. How do i prevent that?
First of all, you’ve got redundant code there. As soon as you notice you could have copy&paste your code or see that all the stuff just differs a little bit or based on some variables, you can create methods for that, shorten the code and gain better readability.
The problem in your code:
When OnTriggerExit is called and you run into the first if, the assignment ‘round=2’ will make sure that the next if’s condition evaluates to true as well.
If you want to stick with the code, use ‘else if’ in the second case.
Well, if you look at your code, you’ll see that you check the tag in every case. What about only checking it once and go through any other condition inside the if? Nesting if statements is equal to the conjunction (‘&&’) of conditions.
That’s not a must, but it is often better to read.
As i don’t know your implementation of your timer, i cannot give any hint on that but it looks as if it could be improved as well. If so, you could have a more general yet more dynamic code which would not require lot’s of changes in case you add more rounds. You may not need that, but if you think about how to generalize it, you’ll improve the ability to find more efficient, more maintainable and shorter solutions and algorithms, which is what you want as soon as your project gets bigger.
(Regarding tags i also recommend to make a class that simply holds constant string variables which represent your tags. Once defined, use the variable instead of a string literal in order to avoid typos and to keep your code easy to maintain. Imagine you rename one tag, and had to change it in a bigger project in every single file and function - it’s way easier to change it just once which applies automatically to everything.)