How do you call a function once
So I Created a function that runs with
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Ocean"))
{
SceneManager.LoadScene("minigame", LoadSceneMode.Additive);
}
}
The problem is it collides about 4 times before the seen is actually loaded, so then the scenes get stacked on top of each other and all run at the same time. I am new to unity and don't know how to fix this. Please help. Once I get this done I will almost have my game finished, so thank you for your help. @SohailBukhari
Answer by TeohRIK · Apr 23, 2017 at 01:41 AM
use boolean
private bool loadSceneCalled = false;
void OnTriggerEnter(Collider other)
{
if(loadSceneCalled)
return;
if (other.gameObject.CompareTag ("Ocean"))
{
SceneManager.LoadScene("minigame", LoadSceneMode.Additive);
loadSceneCalled = true;
}
}
Answer by Bigfootstoe · Apr 23, 2017 at 02:57 PM
Alright, that worked, but I still have a problem. It doesn't close the already open scene, so I still have two running at the same time, the current scene and the old one. How do I fix that? Btw, The answer was beautiful thanks @walaoRIKI
oh...you just change the LoadScene$$anonymous$$ode.Additive to LoadScene$$anonymous$$ode.Single
Or juz call the load scene function like this
Scene$$anonymous$$anager.LoadScene("$$anonymous$$igame");
This still will not work. I need to be able to close the current scene and then run the Scene$$anonymous$$anager.LoadScene("$$anonymous$$igame");
In that order. Thanks
If I do that should I keep the boolean or not? Also, How do you close a scene? Thanks
I need to be able to close the current scene and then run the Scene$$anonymous$$anager.LoadScene("$$anonymous$$igame");
$$anonymous$$aybe you can switch to a blank or black scene or I call it a loading scene first and then only switch to the scene you want
If I do that should I keep the boolean or not? Also, How do you close a scene? Thanks
If you switch to another scene then only switch to $$anonymous$$i game scene then no need the boolean. Usually before I switch the scene I will turn on a black panel with Loading text from UI canvas first then only call the LoadScene function, if I want to have some fancy loading animation then I will use loadsceneasync to load the on blackground.
It works! I reset my computer and its all better!
Your answer
Follow this Question
Related Questions
Scene Reset on HTC Vive Idle? 0 Answers
Scene Editor Problem @username 0 Answers
Why is my scene not loading? 2 Answers
How to check when current scene has loaded? 0 Answers
unity2d One Scene Not Working 0 Answers