Using a button to switch scenes..

I know this is stupid easy..but I simply cannot get this to work and it is driving me bonkers. I was using the online documentation, but the syntax being provided for the commands (hopefully i found the right ones!) is generating errors..

Basically, I have SceneA, which contains Button1.
When I clock Button1 i want to go to SceneB

I find PLENTY of support for Application.LoadLevel but it seems that this is "obsolete" with the version im using (5.3) and ii need to use the SceneManager context...but I am just going in a circle of errors trying to figure out how to do get this working..

Here's a script you can attach to a game object and link to the button:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Control : MonoBehaviour
{
    public void NextScene()
    {
        SceneManager.LoadScene("Blank");
    }
}
9 Likes

Thank you for that speedy reply and MUCH simplified script compared to the beast i had written...(i am an ABSOLUTE beginner, I warn you now...)

So, i have taken your script, and simply replaced the text of mine with yours.

My script is listed as an "Asset".

I went to my button, down to the On Click() area...

I have it set to "Editor and Runtime"
then below that, the "Asset", "Test_Script" has been selected.
And the pulldown on the right is set to "No Function"

But when i try to test my button..i see it press down and then release..but no action takes place.

  • Create an empty GameObject (in my case I named it Controller)
  • Drop the script on it, and make sure the filename is Control.cs
  • Add the object under "On Click ()"
  • Select its method in the rightmost dropdown

It should look a bit like this:

5 Likes

my script needs to be named Control? when I try to do that i get an error:

The namespace 'global::' already contains a definition for 'Control'

and when i try to drop my script, "MyTestScript" on the game object i created (and renamed 'Controller'), i get the error:

Can't add script component 'MyTestScript' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match...

OK, find another name for it and make sure the class is also renamed to the same :)

ok..named game object "Controller" and script "Controller'

i was able to drop my script on the object now. Woot! :smile:

still getting nothing when i test my button. hmm..

Not sure what's wrong, as I followed those exact instruction in a blank project with two scenes. Have you remembered to add the scenes to the build?

no, i hadnt..thats what im screwing around with now..but oddly, the button from the second scene shows up on the canvas of the first scene. lol.

i have so much to learn. thanks for your patience. its much appreciated.

so yeah...something is working. i think i have something screwed up.

i have Scene1 and Scene2 added under my Hierarchy. When i test the game, I see my button from Scene1 and the button from Scene2.

But when I click the button on Scene1, it goes away and I can only see the button from scene2..

i think whats happening is at load, BOTH scenes are visible. once i hit the button, only scene2 is visible.

That's utterly weird. You're certain you're not loading anything additively at some point?

you know..i have no idea. i was doing a lot of stuff messing around trying to self teach myself this. i think i may best off scrapping my playground and starting fresh. :smile:

Yeah, I juggle three computers here just so I can keep one around for blank projects where I test little things :)

ugh! same result. its like my scenes are stacked on top of each other and are transparent!

Share the project?

how? i was trying to upload it..but its saying file too large at just 2mb

Try deleting the Library folder, or find some free filesharing site that works.

deleting the library folder worked. :smile:

2466480--169751--Scene_Selection.rar (36.1 KB)

I found the problems:
-EventSystem was missing from the scene
-Scenes were not added to the build settings