Search Unity

Button doesnt work, need help

Discussion in 'Getting Started' started by myunityprojects, Oct 5, 2019.

  1. myunityprojects

    myunityprojects

    Joined:
    Oct 4, 2019
    Posts:
    1
    Im trying to make a simple mainmenu and Im stuck at a button that I called "newgamebutton".
    Its supposed to start the game after you clicked it (with a left mouseclick), changing the scene from the mainmenu-scene that I called "mainmenu" to another scene that I named "newgame".

    I tried a few different aproaches, but none of them worked. I saw some tutorials and tried to replicate it 1:1 with no success. I deleted everything and now I just pressed add component in the inspector menu of my button "newgamebutton" and put the script that I named "newgamebuttonscript" there. It contains this code:

    using UnityEngine;
    using System.Collections;

    public class newgamebuttonscript : MonoBehaviour {
    void OnMouseDown() {
    Play();
    }
    void Play() {
    Application.LoadLevel("newgame");
    }
    }

    I put the scenes in the build menu: 0 is mainmenu and newgame is 1.
    I just downloaded unity and I use the program "Microsoft Visual Studio Code" with it.
    What do I have to do now and what do I have to change?
    I didnt put anything in the newgamescene so if it works, everything should just turn blue bc thats how the game-mode displays the nothingness of that scene.

    heres a screenshot:

    Screenshot.PNG
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,194
    1. Delete the OnMouseDown method.
    2. Use the OnClick event of your button (in the inspector) to invoke the Play method of your script.
    3. Go through the UI tutorials, like this one.
     
    Joe-Censored likes this.