Loading a scene

I have a “Start Game” button

public static bool GameIsStart;
public void changeCamera ()
{
if (GameIsStart == true)
return;

         GameIsStart = true;
         SceneManager.LoadScene ("Game");
 }

}
`` There is a script that I added to the Prefabs

public GameObject [] gameobject;
private bool gameobject_IS_Spawn;
private float RandomPositionforX;
private int RandomObjects;

private void Update ()
{
    if (StartGame.GameIsStart &&! gameobject_IS_Spawn)
    {
        StartCoroutine (Spawngameobject ());
        gameobject_IS_Spawn = true;
    }
}

IEnumerator Spawngameobject ()

{
    while (true)
    {
        if (StartGame.GameIsStart)
        {
            yield return new WaitForSeconds (1.3f);
            RandomPositionforX = Random.Range (-2.28f, 2.28f);
            RandomObjects = Random.Range (0, gameobject.Length);
            Instantiate (gameobject [RandomObjects], new Vector2 (RandomPositionforX, 24f), Quaternion.identity);
        }
    }
}

}
There is a button on the “Game” scene to access the preview

public void ChangeScene ()
{
SceneManager.LoadScene (“preview”);
}
}
I click on the button, the “Preview” scene is loaded, but the “Start game” button does not work.

If I put in the “Preview” button SceneManager.LoadScene (“Game”) then I restart the scene (“Game”) and everything works. But if SceneManager.LoadScene (“Preview”) but the buttons don’t work. When switching between scenes, is it necessary to stop Coroutine How can this problem be corrected?

unity3d

Hi,
Maybe you can try using build index instead of Scene names (see it in build settings), will avoid some errors.

And for gameobject_IS_Spawn you can also do a simple verification in the coroutine (don’t know if it helps)

Gameobject G = Instantiate (gameobject [RandomObjects], new Vector2 (RandomPositionforX, 24f), Quaternion.identity);

if(G)
{
gameobject_IS_Spawn = true
}

Tell me if it helped you :slight_smile: