I’ve been running into this annoying problem in which my 2nd scene doesn’t load most of the time. It loads about 5% of the time (just a guess). The level isn’t that big and when it does load it takes a second. My code below:
#pragma strict
import UnityEngine.UI;
import UnityEngine.SceneManagement;
public var entryPoint : Transform;
public var loadScreenObject : GameObject;
public var loadingBar : Slider;
private var lvl : int;
private var async : AsyncOperation;
function OnTriggerEnter(col : Collider) {
if(col.gameObject.tag == "Player") {
LoadingScreen(lvl);
}
}
function LoadingScreen(lvl : int) {
loadScreenObject.SetActive(true);
async = SceneManager.LoadSceneAsync(2);
GameControl.control.newPosition = entryPoint;
async.allowSceneActivation = false;
while (async.isDone){
loadingBar.value = async.progress;
yield;
if (async.progress >= 0.9f){
loadingBar.value = 1f;
async.allowSceneActivation = true;
}
}
}
Please let me know if you have any recommendations or suggestions. It should, and has, worked like this. I changed “lvl” to the current “2” in LoadSceneAsync because I thought that was the problem. When I first changed it the code worked once. Now it doesn’t.
Nevermind again. It seems to be an issue with the speed of which I enter my trigger. I have no idea why. If I walk into the trigger it works, but any faster (running, in this case) it doesn’t go into the while() loop. Any suggestions?
Okay, I figured it out. Running into it will fire the Trigger more than once (or the function within) so I had to create a boolean to prevent it from firing twice. The double load of my scene is what initially prevented anything from loading.
Final code in case anyone wants to use it.
#pragma strict
import UnityEngine.UI;
import UnityEngine.SceneManagement;
public var entryPoint : Transform;
public var loadScreenObject : GameObject;
public var loadingBar : Slider;
public var lvl : String;
private var loading : boolean = false;
private var async : AsyncOperation;
function OnTriggerEnter(col : Collider) {
if(col.gameObject.tag == "Player") {
if(!loading) {
LoadingScreen(lvl);
loading = true;
}
}
}
function LoadingScreen(lvl : String) {
loadScreenObject.SetActive(true);
async = SceneManager.LoadSceneAsync(lvl);
GameControl.control.newPosition = entryPoint;
async.allowSceneActivation = false;
while (!async.allowSceneActivation){
loadingBar.value = async.progress;
yield;
if (async.progress >= 0.9f){
loadingBar.value = 1f;
async.allowSceneActivation = true;
}
}
}
@path14 You are a Freaking legend man. No puns intended. You don’t know how much I have been struggling with this problem.
I have been searching every post to find the solution, but couldn’t find any solution and at last!!!. Kudos to you man, thanx again man, really appreciate the solution.