Search Unity

How do I make a button to change the scene in a Unity 3D project?

Discussion in 'Scripting' started by Emolgun, Sep 19, 2020.

  1. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    I need help, i'm making a game, and for the main menu I want to make a button to go to the first level in the title screen. Unfortunately, I'm having some trouble making a button to change the scene on the fly. Being new to Unity, I have no idea what the heck I'm doing. Can I get some help? Thank you! ;)
     
    Last edited: Sep 19, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you're not sure where to start on a problem, try breaking it down into smaller pieces. I'm confident you can find simple tutorials that show you how to write a script that changes the scene, and (separately) how to make a button that calls a script that you wrote.
     
    Emolgun likes this.
  3. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    Ok, I'll try that.
     
  4. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    So I tried it. I linked it to the button, and it seems to work with all the functions and what not, but when I click it, nothing happens. Also, the color of the button doesn't tint. Here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class menu : MonoBehaviour
    6. {
    7.     public void ChangeScene (string sceneName)
    8.     {
    9.         SceneManager.LoadScene("scenename");
    10.     }
    11. }
    Did I do something wrong? Please help.
     
    Last edited: Sep 19, 2020
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Be sure to check the Unity console for error messages. In the code you posted, it looks like you may have forgotten to replace "scenename" with the actual name of the scene you want to load.

    But if there's a problem with the button or linking up the button to your code, then I can't diagnose that by looking at your code; I'd need details of how you created the button and linked it up. It may be more efficient for you to go back through the tutorial step-by-step and make sure you didn't miss anything. Start by just making sure the button does something--probably whatever tutorial you are following will have some example functionality--and only worry about the scene-loading part once you have confirmed that the button itself is working.
     
    Emolgun likes this.
  6. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    I replaced the scenename with the scene and it didn't work. Although, I did make a test button and nothing happened, and I think the button just isn't working. Not even building the game worked. I put the script in a GameObject because if I just put the script in the button it would just show MONOSCRIPT STRING NAME on the functions and when i put it in the gameobject, the functions were showing. There are no errors, though. Here's what the button's configurations look like:
     

    Attached Files:

    Last edited: Sep 20, 2020
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You said not even building the game worked. What happened when you tried to build?

    Anything in the Unity console?
     
    Emolgun likes this.
  8. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    I built the game and when I clicked the button, nothing happened. I didn't mean that it wouldn't build the game. I should have worded that better. Sorry for the confusion.
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Let's see. That looks like a UI button. Does it have a Canvas as an ancestor? Does that Canvas have a raycaster attached?

    Theoretically something could be in front of the button. One of the auto-created game objects (I think it's the input module) will display some debug information at the bottom of the inspector if you select it while in play mode, and one of the things it lists is what the mouse is currently pointed at. Use that to make sure Unity detects you pointing at the button.

    It also theoretically could be something dumb, like accidentally trying to click the button in the scene view instead of the game view. Without actually watching what you're doing, it'd be hard to notice mistakes like that.

    If you still can't click the button, try creating a new scene, say new > UI > Button, click play, and see if that will change color for you (without adding or configuring anything else).
     
    Emolgun likes this.
  10. Emolgun

    Emolgun

    Joined:
    Sep 4, 2020
    Posts:
    11
    Oh, found the problem. There wasn't an event system, which was weird because it would usually come with the button. Everything is working now. Thanks anyway! ;)