- Home /
Change scene using text UI ? help
I want to change scene by the content of Text UI when I press Start
I add my C# script to Main Camera and give Text to C# script
and call changemenuscene when I press button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Changescene : MonoBehaviour {
public Text Scenename;
public void changemenuscene ()
{
//Debug.Log("Hi");
Debug.Log (Scenename.text);
Debug.Log (SceneManager.GetActiveScene ().buildIndex);
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
if (Scenename.text == "Dog") {
Debug.Log ("Dogg");
//Application.LoadLevel (1);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
else if (Scenename.text == "Cat") {
Debug.Log ("cat");
//Application.LoadLevel (0);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
}
}
}
Debug.Log
Dog
UnityEngine.Debug:Log(Object)
Changescene:changemenuscene() (at Assets/Changescene.cs:13)
UnityEngine.EventSystems.EventSystem:Update()
0 UnityEngine.Debug:Log(Object)
Changescene:changemenuscene() (at Assets/Changescene.cs:14)
UnityEngine.EventSystems.EventSystem:Update()
in the yellow square fail to use if and else if, and I don't know how to solve this problem
thanks for your time to help
What problem are your facing exactly? Who calls the changemenuscene
function? How? Have you any error in the console? Do you see the output of the Debug.Log
?
Please, remove the code image and copy-paste your actual code using the 101010 button.
I edit it can you help me check again? Thanks a lot
Hi, Are you sure the "changemenuscene()" function is getting called whenver required ?. If so make sure the scene name is spelled correct in the if-else and also dont forget to add all the scenes to "Scenes In Build" under File->BuildSettings
Try this function in order to compare :
if( String.Compare(Scenename.text.Trim(),"Dog", StringComparison.OrdinalIgnoreCase) == 0 )
Answer by NinjaNeko · Dec 11, 2017 at 10:26 AM
Correct version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Changescene : MonoBehaviour {
public Text Scenename;
string String1 = "Dog" ;
string String2 = "Cat";
public void changemenuscene ()
{
//Debug.Log("Hi");
Debug.Log (Scenename.text);
Debug.Log (SceneManager.GetActiveScene ().buildIndex);
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
if( string.Compare(Scenename.text.Trim(),String1)==0 ) {
Debug.Log ("Dog");
//Application.LoadLevel (1);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
else if( string.Compare(Scenename.text.Trim(),String2)==0) {
Debug.Log ("cat");
//Application.LoadLevel (0);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
}
}
}