I need help with Unity. I am trying to load in another scene when the condition is cleared, but after testing it, I can’t find the problem. Can I get an answer for this? Here is the script, and the separate scenes are PlayingField1 and PlayingField2, attempting to load from PlayingField1 to PlayingField2. Thanks.
Forgot to add the script, here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
private float speed = 12;
public Text countText;
public Text winText;
public Scene PlayingField2;
private Rigidbody rb;
private int count;
// Use this for initialization
void Start()
{
rb = GetComponent();
count = 0;
SetCountText();
winText.text = “”;
}
//called per frame, before peforming physics
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag(“PowerUp”))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winText.text = “Next Level!”;
SceneManager.LoadScene(sceneName: (string)PlayingField2);
}
}
}
Use the code button to paste code into your posts.
Try
Print("Loading");
before the scenemanager line. If it prints the problem is your scene manager line.
Try
SceneManager.LoadScene("PlayingField2");
Instead. If that doesn’t work make sure your scene is built in your project settings.
please use code tags to ensure that the code you show us is more readable.
To track down the issue, you should add some
Debug.Log("some pertinent info");
and watch the console log what is printed out so you know that your code is actually loaded. Insert the debugs right before invoking LoadScene
Something non-obvious: you MUST have added the scene that you want to load to the build list in the Project Settings, else the load will fail.
Oh, and in
SceneManager.LoadScene(sceneName: (string)PlayingField2);
It looks as if you are casting a scene type object to a string. That looks fishy to me.
Hi and welcome!
As others suggested, the problem lies in the above line. I thought casting a Scene to a string may not return what you expect it to… but i was wrong. It returns nothing, because it does not even compile. So are you sure your project throws no errors for that line? Because for me it certainly did tell me that it cannot convert from Scene to string. Generally, when you want to get a string from some object, call ToString() on it. However, the result depends on whatever was implemented as return value for ToString of this type.
Another thing i noted, is that you cant even assign Scenes through the inspector like you seemingly plan on doing in:
public Scene PlayingField2;
I didnt even know that, but jeah, doesnt really work all that great, as the inspector only shows you a field for the Handle.
So as others suggested before, just type it out and use SceneManager.LoadScene(“PlayField2”);
You can also remove the public Scene PlayField2; line at the top, since you wont need it anymore.
Also, if you have errors (and you should have!), it’s always a good idea to post the full error messages so people can help you faster. And please use code tags to post code examples. This adds syntax highlighting and makes it easier to read. It’s the <> button at the top of the textfield you write your posts in. You can also read up on using code tags in the first sticky on this subforum.