How can I check if the level is loaded in Unity3D

Luccas Gabriel

I've started learning C# and Unity3D this year and I've come across a problem. I'm loading a new level and the player object gets initialized before the the level has finished loading.

I think I only need to check whether the level is loaded or not before initializing the object. I don't know if I could use LoadLevelAsync. I'm using Unity3D Personal Edition.

My current code:

MasterClient.cs

void Update(){
    if(SceneUpdateRequired)
    {
        Application.LoadLevel(SceneUpdateID);
        if (Application.isLoadingLevel == false)
        {              
            SceneUpdateRequired = false;
            SceneUpdateCompleted = true;
        }
    }
}

void CharacterLoginUpdate(){
    if (SceneUpdateCompleted == true)
    { 
        GameObject networkPlayerObj = (GameObject)Instantiate(NPlayerObj, PlayerLoginUpdatePosition, PlayerLoginUpdateRotation);
        NPlayer networkPlayer = networkPlayerObj.GetComponent<NPlayer>();
        networkPlayer.UpdatePlayerInstantiateCompleted(PlayerLoginUpdateNetworkID, PlayerLoginUpdateHP, PlayerLoginUpdateClass, PlayerLoginUpdateLevel, PlayerLoginUpdateName, PlayerLoginUpdatePosition);
        PlayerLoginUpdateRequired = false;
    }
 }
killer_mech

The problem with your code is your are loading level in update where if you dont control the task with booleans you are going to end up with big problems. your update code should be as follows

 void Update(){
    if(SceneUpdateRequired)
    {
        Application.LoadLevel(SceneUpdateID);
        SceneUpdateRequired = false;        
    }
    if (Application.isLoadingLevel == false)
    {              
        SceneUpdateRequired = false;
        SceneUpdateCompleted = true;
    }
 }

This way the scene code will try to load level only when you request it to and not every time in update loop as loading levels is heavy.

Another thing is you might encounter one more problem if you need to use SceneUpdateRequired,SceneUpdateCompleted variables somewhere else the

if (Application.isLoadingLevel == false)
{              
    SceneUpdateRequired = false;
    SceneUpdateCompleted = true;
}

the above part which is in update loop will reset the variables everytime whenever its not loading the level. To prevent this you will have to introduce another boolean flag to stop its update happening everytime. So your code will end up looking like this if you want to prevent everyframe update

void Update() {
    if(SceneUpdateRequired){
        Application.LoadLevel(SceneUpdateID);
        SceneUpdateRequired = false;
    }
    if (Application.isLoadingLevel == false && StartCheckingLoadLevel){ 
        SceneUpdateRequired = false;
        SceneUpdateCompleted = true;
        StartCheckingLoadLevel = false;
    }
}

void StartLoad() {
    SceneUpdateRequired = true;
    SceneUpdateCompleted = false; 
    StartCheckingLoadLevel = true;
}

void CharacterLoginUpdate(){
    if (SceneUpdateCompleted == true) {
        Debug.Log("Do Something after load");
    } else {
        Debug.Log("Scene not yet loaded");
    }
}

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML - How can I check if a file (.js or .css) was loaded or picked up from the cache?

From Dev

How do I check if an already loaded image has loaded in JavaScript?

From Dev

Building an orthographic camera for Unity3D, how can I keep four specific targets in sight?

From Dev

Unity3D canvas. How I can get camera?

From Dev

How can I find the rotation of the head in Google Cardboard Unity3D?

From Dev

How can I output Unity3D debug log?

From Dev

how can I pass a C# component from one gameobject to another in Unity3d

From Dev

How can I detect a shake motion on a mobile device using Unity3D? C#

From Dev

How can I check if an ajax .load call has loaded

From Dev

UNITY3D How check direction vector of my camera?

From Dev

How Can I Package a Unity3D 5 Windows Application into a single exe file?

From Dev

How can I check permission under API level 23?

From Dev

How can I check if a bean has been loaded by springboot

From Dev

How can I use Clojure-CLR on Unity3d?

From Dev

How can I parse .unity3d file on sever side by php

From Dev

How can I mute specific audio sources in Unity3D?

From Dev

How can I find the rotation of the head in Google Cardboard Unity3D?

From Dev

How can I load another level in unity3d?

From Dev

how can I pass a C# component from one gameobject to another in Unity3d

From Dev

In vim with pathogen, how can I check if a plugin will be loaded?

From Dev

How Can I Package a Unity3D 5 Windows Application into a single exe file?

From Dev

VB.NET How can I check if an image from my resources is loaded in a PictureBox?

From Dev

How can I check if jquery is loaded from external JS file?

From Dev

How can i know touched which game object with Unity3D

From Dev

How can I force an orientation per scene in Unity3D

From Dev

How can i raise the wall in unity3d once and control the raising speed?

From Dev

Using jQuery, how can I check when all images have loaded within a dynamic dom structure?

From Dev

How can i get effect shown in video attached in link below in unity3D?

From Dev

How to check if the smartphone has gyroscope in Unity3D?

Related Related

  1. 1

    HTML - How can I check if a file (.js or .css) was loaded or picked up from the cache?

  2. 2

    How do I check if an already loaded image has loaded in JavaScript?

  3. 3

    Building an orthographic camera for Unity3D, how can I keep four specific targets in sight?

  4. 4

    Unity3D canvas. How I can get camera?

  5. 5

    How can I find the rotation of the head in Google Cardboard Unity3D?

  6. 6

    How can I output Unity3D debug log?

  7. 7

    how can I pass a C# component from one gameobject to another in Unity3d

  8. 8

    How can I detect a shake motion on a mobile device using Unity3D? C#

  9. 9

    How can I check if an ajax .load call has loaded

  10. 10

    UNITY3D How check direction vector of my camera?

  11. 11

    How Can I Package a Unity3D 5 Windows Application into a single exe file?

  12. 12

    How can I check permission under API level 23?

  13. 13

    How can I check if a bean has been loaded by springboot

  14. 14

    How can I use Clojure-CLR on Unity3d?

  15. 15

    How can I parse .unity3d file on sever side by php

  16. 16

    How can I mute specific audio sources in Unity3D?

  17. 17

    How can I find the rotation of the head in Google Cardboard Unity3D?

  18. 18

    How can I load another level in unity3d?

  19. 19

    how can I pass a C# component from one gameobject to another in Unity3d

  20. 20

    In vim with pathogen, how can I check if a plugin will be loaded?

  21. 21

    How Can I Package a Unity3D 5 Windows Application into a single exe file?

  22. 22

    VB.NET How can I check if an image from my resources is loaded in a PictureBox?

  23. 23

    How can I check if jquery is loaded from external JS file?

  24. 24

    How can i know touched which game object with Unity3D

  25. 25

    How can I force an orientation per scene in Unity3D

  26. 26

    How can i raise the wall in unity3d once and control the raising speed?

  27. 27

    Using jQuery, how can I check when all images have loaded within a dynamic dom structure?

  28. 28

    How can i get effect shown in video attached in link below in unity3D?

  29. 29

    How to check if the smartphone has gyroscope in Unity3D?

HotTag

Archive