LoadScene will only work from first scene
I am using SceneManager trying to move to the next scene. I am pretty new to Unity so I have taken the roll ball tutorial and added some scenes to it. Now I want to be able to add a new collision that will move to the next scene after all the objects are picked up. I added a new game object and tagged it "Goal".
I added at top: using UnityEngine.SceneManagement;
I added two new variables to track when to move to the next level (I know it is not efficient, but I have tried many variations -- this is just the last)
private bool moveOn;
private bool moveOnNow;
So now the start function looks like this:
void Start() { rb = GetComponent(); SetCountText(); winText.text = ""; moveOn = false; moveOnNow = false; }
Fixed Update function looks like:
void FixedUpdate() { Scene loadedLevel = SceneManager.GetActiveScene();
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (moveOnNow == true) {
SceneManager.LoadScene ("myRoll0"+nextSceneIndex);
}
Debug.Log(nextSceneIndex); (This does show the correct scene to move to)
}
void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Pick Up")) { other.gameObject.SetActive(false); count = count + 1; SetCountText(); }
The second variable becomes true when the play contacts the goal
if (other.gameObject.CompareTag("Goal"))
{
if (moveOn == true) {
moveOnNow = true;
}
}
}
The first variable becomes true when all objects are collected:
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 7)
{
winText.text = "Use the portal";
moveOn = true;
}
}
As I say, not the most efficient way to get it done, but I don't get better results other ways. It will work in the first level, but not in any of the others. I must be missing something -- but I can't find what.
Can anyone help?
Answer by tormentoarmagedoom · May 08, 2018 at 08:50 AM
Good day.
Did you add the scenes in the build settings?
You need to add all scenes that can be loaded! In editor and in build files!
Bye!
Well now I feel silly -- I knew it had to be something simple I was missing. I thought I had tried that at some point and it didn't work -- but now does! Thanks so much!