1

I'm trying to save/load my game. In the load method, everytime I change the properties of a GameObject, those changes are applied and then get reverted shortly after. Here is my code:

public void Load()
{
    SceneManager.LoadScene(sceneID);
    List<GameObject> rootObjects = new List<GameObject>();
    Scene scene = SceneManager.GetActiveScene();
    scene.GetRootGameObjects(rootObjects);

    int ncube = 0, npick = 0;
    for (int i = 0; i < rootObjects.Count; ++i)
    {
        GameObject obj = rootObjects[i];
        if (obj.CompareTag("Player"))
        {                                
            obj.transform.position = player.position;
            obj.transform.rotation = player.rotation;
            obj.transform.localScale = player.localScale;
        }
        else if (obj.CompareTag("Cube"))
        {
            obj.transform.position = cube[ncube].position;
            obj.transform.rotation = cube[ncube].rotation;
            obj.transform.localScale = cube[ncube].localScale;
            ++ncube;
        }
        else if (obj.CompareTag("Pickup"))
            obj.SetActive(pickup[npick++]);
        else if (obj.CompareTag("Door"))
            obj.SetActive(door);
        else if (obj.CompareTag("GreenWall"))
            obj.SetActive(greenWall);
    }
}

Those changes are applied to the GameObject, however they get aborted right away. How can I resolve this?

The script contains these lines of code is not a component of the GameObject.

Edit 1: Complete code updated.

9
  • How do you call this if it is not a component? Commented Jan 16, 2019 at 12:21
  • It's a component of another object (not the ones that I'm trying to change its properties). To be specific, this is a part of my load button. If the user clicks the load button, this script will be prompted and it will try to load the properties of the GameObjects that belong to the active scene. Commented Jan 16, 2019 at 12:26
  • 1
    Do you have another scripts which accesses these properties? Commented Jan 16, 2019 at 12:34
  • Yes, those GameObjects have there own scripts for controlling. One of those has a RigidBody for receiving user input (roll a ball). Commented Jan 16, 2019 at 12:37
  • what else does this load button do? Is there a scene change involved? Commented Jan 16, 2019 at 12:50

2 Answers 2

4

Problem is I think that

Scene scene = SceneManager.GetActiveScene();
scene.GetRootGameObjects(rootObjects);

gets the objects from the scene before the Scene is fully loaded so they are reset.

From the Docu

When using SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.


I guess you rather should use SceneManager.sceneLoaded and do your stuff there like

public void Load()
{
    SceneManager.LoadScene(sceneID);
}

And maybe in an extra component within the scene:

void OnEnable()
{
    SceneManager.sceneLoaded += OnSceneLoaded;
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
    // your stuff here
}

void OnDisable()
{
    SceneManager.sceneLoaded -= OnSceneLoaded;
}

though we don't know/see where player, cube[ncube] etc come from ...

for transparting values between Scenes you should get into using ScriptableObjects

Sign up to request clarification or add additional context in comments.

Comments

2

The problem might be that SceneManager.LoadScene completes in the next frame. See the documentation: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

It says:

When using SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.

You change the values within the same frame, thus they are overwritten when loading the scene finishes. Cou could use an event to prevent that behaviour: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.