Search Unity

Problems with changing scene via the use of a button

Discussion in 'Scripting' started by Hotpots, Oct 6, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey, So I'm totally stumped over my problem. I have written the following code below:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChangeScene : MonoBehaviour
    5. {
    6.     public void NextLevelButton(string levelName)
    7.     {
    8.         Application.LoadLevel(levelName);
    9.     }
    10. }
    I have created an empty game object and attached my script to it. I have clicked the "+" icon on the button script component and dragged and dropped my empty game object containing my script onto the object box. I have selected the function ButtonNextLevel.NextLevel(string). I then fill in the text scene "MainMenu" which is the name of the scene I want to transition too. And then... nothing happens...

    Now I have added it to the build settings and YES it is spelt correctly! :D

    An the other thing is that all my buttons work on the main menu screen to transition to controls/instructions/play game etc, but from any other screen... NOPE it does not want to work.

    P.S. I do also get this error after building and running the project -http://i.imgur.com/u1tgg7i.png
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    run Debug.log on the button function to see if it is running the function when clicked. that will help narrow down the problem.
    Code (CSharp):
    1. public void NextLevelButton(string levelName)
    2.     {
    3.             Debug.Log("Clicked");
    4.             Application.LoadLevel(levelName);
    5.     }
    if the debug doesnt show the it is a problem with the onClick event
     
  3. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Craig!
    In the Debug window it returns "Clicked" on any button clicked on the main menu, however on any other scene "Clicked" is not returned, do you know what could be causing this? Or what I can do to fix this?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    sounds like you've only setup the OnClick event for the button in the main menu scene. The buttons in the subsequently loaded scenes also need to be setup. Unless you are using "DontDestroyOnLoad" or similar function each scene load is an entirely new thing. Nothing from the previous scene is retained.
     
  5. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Lefty,
    Here is my component window for my MainMenuButton in my "BasicGame" scene, you can see how I have set up my button here, and the name of my scenes.



    I honestly can't see what I've done wrong.

    P.S. What exactly do you mean set up the button and OnClick event? And nope I'm not using the DontDestroyOnLoad() function for each scene.
     
    Last edited: Oct 6, 2015
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    this little panel is how you configure a UnityEvent in the inspector; UI buttons come with an OnClick event by default, but it'll look the same for any public/exposed UnityEvent.

    If you're sure each button in each scene has it's OnClick event configured... does each scene have an EventSystem? that should be created when any UI element is first created in the scene, but without it no UnityEvent will work (including OnClick for the buttons)...
     
    lefmecoder likes this.
  7. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Lefty,
    You were correct in saying that there wasn't an EventSystem in those scenes where the buttons weren't working.
    Therefore I just copy and pasted the object into each scene and I am now happy to say the problem has been solved!
    So thank you! :D

    P.S. After I build the project and run it, do you know why I get this error?

    The project still works if I delete the build but yeh... :S

     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    That looks to be a problem with visual studio... I've not run into that myself. A quick googling for "unity visual studio keynotfoundexception" brings up a fair few posts starting a few years back... solution steps like "reimport project" "reinstall unity" "reinstall visual studio"... :(

    Are you using a fresh install of the latest unity version? did you install VS from the unity installer? etc.
     
  9. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Yep,
    I installed Unity roughly 2 months back, so it is Unity 5.1
    I also actually installed Visual Studio yesterday from the Microsoft website however I did get this error before then.