- Home /
OnTriggerEnter to load Level and display message help...
Hello guys,
I am using OnTriggerEnter to load next level as player enters an area, what i am having trouble with is displaying text on screen to alert player that next level is loading...Can you help??
function OnTriggerEnter (col : Collider) {
print("Loading Next Level");
yield WaitForSeconds (5);
Application.LoadLevel (3);
}
shouldn't this at least display the text "Loading Next Level" on screen? Or is the WaitForSeconds delaying everything including the text? Countdown isn't as important, just need it to at least put text on screen to inform player that level is loading, but "print" or "guiText" not working....
this won't display the text on screen but rahter in the console.Use a GUI text or something
Answer by Sun_Glasses_Guy · Oct 18, 2011 at 11:12 PM
You could just make a level load that's just a loading screen, with 3dText centered on the camera while the actual level loads. //At the top of the Screen: GameObject -> Create New -> 3dText(Gui Text might also work)
and then have THAT loading screen load your next level! Then you could throw in a cool splashscreen while the next level loads, although to be honest, if I ever had to load a unity level for more than a few seconds it's usually too choppy to even play
I was thinking this as well, but would still like to learn how to trigger GUI with OnTriggerEnter...
Thanks!
Answer by xmediagrafx · Oct 19, 2011 at 12:33 PM
From Unity 3d forum - create a small game object, with a script attached, that sets DontDestroyOnLoad in its awake function. This game object displays your text for "please wait, loading..." and then after a few seconds, and definitely after the level has loaded, the game object destroys itself.
Code:
function OnTriggerEnter(col : Collider)
{
GameObject.Instantiate(yourLoadingGameObject, Vector3.zero, Quaternion.identity);
Application.LoadLevel(3);
}
Then stick this script on whatever object you want to instantiate just before you load your level. Code:
public class LoadingMessage : MonoBehaviour
{
void OnLevelWasLoaded(int level)
{
Destroy(this.gameObject, 3.0f);
}
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
void OnGUI()
{
GUI.Label(new Rect(50, 50, 200, 25), "Loading next level...");
}
}