On Trigger Exit Help
I'm trying to Load scene when both balls have entered, but it only loads scene when one of the balls have entered.
I set a Bool flag for each Ball for a GameOver component.
I put a Ball component attached to each ball True/False. Set Flag on True onTriggerExit, Set Flag False onTrigger exit.
I set the TriggerEnter set both flags true call load scene
I'm new to coding any help or guidance is much appreciated.
Post the actual code. That will make finding the problem a 100 times easier.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class Bool : $$anonymous$$onoBehaviour {
public GameObject ball1;
public GameObject ball2;
public bool ballBool;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("hitWall"))
{
ball1.SetActive(true);
ball2.SetActive(true);
}
{
if (ballBool) ballBool = false;
else ballBool = true;
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class GameOver : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other)
{
if ((other.tag == "Ball") || (other.tag == "Ball2"))
{
Scene$$anonymous$$anager.LoadScene("GameOver");
}
}
}
I completely forgot about the code, sorry about that.
Answer by MacDx · Feb 15, 2018 at 06:57 PM
Ok. Here are the problems I can see in your code.
1) There is a logical problem with this bit in your GameOver class
void OnTriggerEnter(Collider other)
{
if(other.tag == "Ball" || other.tag == "Ball2")
SceneManager.LoadScene("GameOver");
}
You say that you want to go to GameOver scene when both balls have entered the trigger, but this code does not do that, this codes makes you go to GameOver when either of the balls enter the trigger, why? Because of the condition you're using. Right now, your code says something like If I touched ball1 OR ball2 then change scene. But what you want is different, you want your code to say if I have touched ball1 AND ball2 then change scene. How to do this? well first, your code needs to remember that you have already touched the ball, an easy way to do it is with boolean flags. You would add a boolean flag for ball1 and ball2 then check that in your if statement. Like this:
public bool ball1Flag;
public bool ball2Flag;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Ball")
ball1Flag = true;
else if(other.tag == "Ball2")
ball2Flag = true;
if(ball1Flag && ball2Flag)
SceneManager.LoadScene("GameOver");
}
This will work, however, it is actually a bad approach to solve the problem because it is not flexible. And this takes us to the second issue.
2) What if you want to add more balls? Let's say you want to have 20 balls now. Are you going to declare 20 boolean fields ball1Flag, ball2Flag, ball3Flag .... up to ball20Flag? And create tags for each of them? Never, you should never think of that. Remember that programmers should hate repeating themselves when writing code, and writing public bool ballNFlag; 20 times is repetitive isn't it? So, how to solve this? Consider having just one tag and one field but instead of a bool field let's make it an integer field, a counter, and all balls will share the same tag ("Ball").
public int ballCounter;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Ball")
ballCounter++; //Increments counter by 1
if(ballCounter >= 2)
SceneManager.LoadScene("GameOver");
}
This will work too and it is a much better solution because you can easily change the number of balls later, and now you don't even need to have all those booleans to tell you which ball is which, you just care that 2 balls entered.
Hope this helps!
One final note. As a programmer I advice you to stay away from booleans as much as possible, the more booleans you use, the harder it will be to understand the code you write. I know you said you're new to coding but it is very important that you learn about good coding practices when writing object oriented code.
Regards :)
It works great, thank you very much for your guidance, You made some great useful tips on what you wrote.
Sorry to bother you again I was wondering how do you get the one script to work the same with other gameObjects. When I give each gameObject there own individual script the ball is only triggered to that script but not the others. Any advice on how to get all of the gameObjects to work with one script? @$$anonymous$$acDx
Ah, so you want to have multiple of these game over triggers and you want them to work as if they were one. What I would usually do is have a manager or some kind of object that overviews every trigger. The logic would be exactly the same you just need to delegate every OnTriggerEnter call to this $$anonymous$$anager object (by delegate I mean to pass the result of every OnTriggerEnter call that happens, to this $$anonymous$$anager and let the manager process it ins$$anonymous$$d of doing the checks in the trigger's script).
Hey it's me again, I had no success on trying to get the one script to work with all of the gameobjects. I looked at various tutorials and I followed your instructions but I'm a beginner at coding. Is there any good references or advice you can help me out on. Again, thank you for your help previously. @$$anonymous$$acDx
Your answer
Follow this Question
Related Questions
How to set a bool from a script equal to another bool from another script? 1 Answer
How to get onTriggerEnter to control a bool 0 Answers
how do i get a boolean to switch to true only after i've pressed a key three times? 2 Answers
Referencing non-static variables 1 Answer
If statement not working 1 Answer