Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using a button to switch scenes..

Discussion in 'Scripting' started by Alcedes, Jan 16, 2016.

  1. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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..
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Here's a script you can attach to a game object and link to the button:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class Control : MonoBehaviour
    5. {
    6.     public void NextScene()
    7.     {
    8.         SceneManager.LoadScene("Blank");
    9.     }
    10. }
     
  3. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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.
     
    Last edited: Jan 16, 2016
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    • 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. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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...
     
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    OK, find another name for it and make sure the class is also renamed to the same :)
     
  7. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    ok..named game object "Controller" and script "Controller'

    i was able to drop my script on the object now. Woot! :D
     
  8. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    still getting nothing when i test my button. hmm..
     
  9. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    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?
     
  10. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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.
     
  11. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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.
     
  12. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    That's utterly weird. You're certain you're not loading anything additively at some point?
     
  13. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    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. :D
     
  14. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Yeah, I juggle three computers here just so I can keep one around for blank projects where I test little things :)
     
  15. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    ugh! same result. its like my scenes are stacked on top of each other and are transparent!
     
  16. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Share the project?
     
  17. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    how? i was trying to upload it..but its saying file too large at just 2mb
     
  18. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Try deleting the Library folder, or find some free filesharing site that works.
     
  19. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    deleting the library folder worked. :D
     

    Attached Files:

  20. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    I found the problems:
    -EventSystem was missing from the scene
    -Scenes were not added to the build settings
     
  21. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    i was doing something different...i wasnt doing the build and run...on the UI, next to the "Scene" tab there is a "Game" tab...i thought i could test things right there. is that not correct?
     
  22. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
  23. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    i got every thing working when i used the "Build and Run" option.
    Will I need to create a script like that for every button that switches scenes? surely there is a way to pass data to the script and have it switch to a scene based on the data passed...

    back to the tutorials! :D


    and also, THANK YOU so much for all the help. i actually gained quite a bit of understanding from that. :D
     
  24. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Yes, it's simple. Just change the method like this:
    Code (CSharp):
    1. public void NextScene(string scene)
    2.     {
    3.         SceneManager.LoadScene(scene);
    4.     }
    A text field will now appear in the inspector, where you can type in the name of the scene.
     
    guerrero7 likes this.
  25. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    completely off topic..how come some times my canvas seems to get locked...i cant resize it or reposition or anything.

    like right now, i cant manipulate a canvas at all. even if i create new one. all its size and positioning attributes are greyed out.
     
    Last edited: Jan 16, 2016
  26. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    I haven't come across that myself. Check the latest patch release if you are just using an f-release.
     
  27. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    perhaps i have the wrong mentality here...does every "Scene" need a "Canvas" for me to construct a UI on?
     
  28. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Yep, one or more canvases per scene. Generally you'll manage with one, but for some advanced things it's handier to have multiple and just hide/show them for different purposes. You can also create prefabs from them if you have some repeating GUIs.
     
  29. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    im going to get some rest. but i keep running into issues with my canvas' being locked to their current position and size....this will be a major hindrance if i cant figure out what is causing it
     
  30. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Well, the canvas itself isn't supposed to move. Consider it a representation of the screen area you can put GUI controls on. For moving parts you use panels and buttons attached to those.
     
    adeeshaxd likes this.
  31. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    im doing something totally wrong and i just cant figure it out.
    When i make a Scene. I create a Canvas. on this Canvas, I create my UI...

    Then I add anoother Scene...when i add the Canvas to this scene, it always aligns EXACTLY with the first Canvas. and I can see the UI controls from the Canvas when I am trying to construct the UI on the Canvas of Scene2...

    The reason I wanted to move the Canvas was so that I could see wtf I am doing
     
  32. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    No idea what's going on there. Which version are you using?
     
  33. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  34. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    im using 5.3f
     
  35. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Try the latest patch release? I've seen a lot of posts complaining about GUI bugs lately.
     
  36. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    its probably working exactly the way it is suppose to and im just not smart enough to understand it.

    When I add two scenes to my hierarchy, i only use 1 canvas? or do i use a canvas on each scene?
    and then, when i am on scene1, why do i see the buttons from scene 2 still? and vice versa. when im trying to work on scene2, i still see the buttons from scene1.

    how in the heck are you suppose to design a ui if everything is visible from all scenes at once?

    i must be screwing something up. i wish there was a way to show you exactly what im seeing. UGH! i really want to do this but im making no progress.
     
  37. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    I'm not sure why you're seeing canvases from different scenes in a current scene, but a canvas is tied to a scene. You create a scene, create a canvas on it and all the controls you need. This is not what I'm seeing, so something is screwy with your installation. I'm using 5.3.1p3 on OS X 10.11.2 to be specific.
     
  38. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    i have 5.3.1f1 on Win10

    im patching now.
     
  39. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I'm on the latest release on Windows 10 & I'm having no issue with buttons, UI or scenes.

    Watch the unity UI tutorials, it might be something as simple as the way you are building your scenes & UI.
     
  40. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    ya, im giving up. im not getting any where. im not seeing anything in the tutorials or google searches.

    i have added 2 scenes to the hierarchy.

    I go to scene1, then i add a canvas to one. the canvas cannot be moved. and it cannot be resized. then i add a panel to the canvas and then a button to the panel.

    then

    I go to scene2, the canvas of scene1 is still visible. along with the panel and button.
    i add a canvast to scene2. the canvas2(attached to scene2) is exactly the same size and in the exact same position as canvas1(on scene1). it also cannot be resized or moved. i add a panel2 to canvas2(which is attached to scene2) and then add a button2 to panel2 of scene2.

    Now when i look at scene1 or scene2, i see BOTH panels. and BOTH buttons. honestly, unless i find someone who knows wth they are doin, that can look at this with me over teamviewer or something...i think this is a stopper for me. i have spent ALL DAY messing with it.
     
  41. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    Just remove any important/confidential things from your project. Zip it and upload the project here. I will take a look at it. I will fix and re-upload it here. You can use http://wikisend.com/ if the file is too big. No registration required.

    All you want if for scene to change when you press the button?
     
    Last edited: Jan 17, 2016
  42. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Upload a screenshot of each of your scenes so we can see the hierarchy as well
     
  43. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    that is what it started as hangemhigh...but now im just confused on how i can design ui's for each scene when my scenes added to the heirarchy seem to be transparent..and canvas's cant be used to unstack...
     
  44. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    cant seem to upload my project..size restrictions...(it is 180mb, with library folder removed 180mb)
     
  45. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What's the smallest project you can create that still has the problem?

    Try just uploading a screenshot of each scene so we can see what you are talking about
     
  46. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    I think that too many people have tried to help with a picture you already uploaded and that didn't help at-all. That's why I am asking for the whole project. You can remove sound files, models and their textures if you think anyone is going to steal it. If you are going to remove any models, make sure to replace them with dummy cubes from Unity Engine. Try https://www.fileconvoy.com/ The limit is 3029 MB. Also fully describe your problem. It looks like you got the "press button, load new scene working" and you are having problem with transparency.

    You said "canvas's cant be used to unstack". Not sure what you meant by that. Explain more after uploading scene. For example, what are you trying to do now?
     
  47. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    I know its something i am doing wrong...and its something stupid and simple.
     

    Attached Files:

  48. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    There's some sort of new function that lets you build multiple scenes at the same time or something like that. I don't use it but I've seen someone else with a similar issue of multiple scenes being displayed at the same time & that was the cause. Check the release notes to see if you can find anything about it.
     
  49. Alcedes

    Alcedes

    Joined:
    Jan 16, 2016
    Posts:
    25
    still looking but i cant seem to find anything.
     
  50. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570