Change Scene (Unity 5, C#)

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class DictatorTeleport : MonoBehaviour
{
    void OnGUI()
    {
        SceneManager.LoadScene("CityScene", LoadSceneMode.Additive);
        SceneManager.SetActiveScene(SceneManager.GetSceneByName("CityScene"));
    }
}

This doesn’t work. How would I make it change to my other scene called “CityScene” on GUI button click?

did you set the scene in the build settings

also, unless you’re really attached to the old way of doing things, I’d suggest looking into the new Canvas based UI

You also might want to create… a button.

Yep… I have a button.

Trying to use the new UI settings for C#… but I can’t find anything on how to do what I want.

Errors:

Scene ‘CityScene’ (-1) couldn’t be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
ArgumentException: SceneManager.SetActiveScene failed; invalid scene
UnityEngine.SceneManagement.SceneManager.SetActiveScene (Scene scene)

Also, why can I still click buttons after deactivating them?

It tells you what is wrong…
Go to the build settings - (where you go to export the game) and drag your scene into the scene list.

Try unticking the “Interactable” option on the button.
http://docs.unity3d.com/ScriptReference/UI.Selectable-interactable.html

What he mean here is that OnGUI() is used for the legacy UI system where you would manually create a button within code. for the new system you should simply use a Public function and attach it to the OnClick() option on the button.

how to do buttons in the new UI
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button?playlist=17111

Alright, thanks. That makes sense. But how would I make an Interactable function from in here? I don’t see any option that lets me create an Interactable function, like how I can make a SetActive function. Sorry to answer answers with more questions. See image below.

I’ll try that. Thanks.

2631899--185015--Capture.PNG

Works, thanks. I’m kinda new to Unity.

http://docs.unity3d.com/ScriptReference/UI.Selectable-interactable.html

you can set the interactable property directly in code.

I dont know exactly what you are doing with those scipts on the OnClick() but if you want to make it so that a button cannot be interacted with simply do:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ForumTest : MonoBehaviour
{
    // A reference to the button
    public Button test;
   
    void Update ()
    {
        // What will trigger this?
        // EDIT THIS!
        if(conditionIsMet = true)
        {
            // If the condition is met than set the button to be interactable!
            // Easy!
            test.interactable = true;
        }
    }
}

Thanks.

How would I get the children of a parent?