I am brand new to using Unity and Coding in general. I have been watching and using many tutorials, and I have a long way to go on learning these things.
I started a project, just simple maze game, and I am trying to add a main menu with the “start” and “quit” button.
I am using this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainMenu : MonoBehaviour {
public bool isStart;
public bool isQuit;
void OnMouseUp(){
if(isStart)
{
Application.LoadLevel(1);
}
if(isQuit)
{
Application.Quit();
}
}
}
The buttons are marked as “isStart” and “isQuit” in the check boxes.
But when I go to game mode or build and run the buttons don’t do anything. What am I missing?
Thanks for the help guys
The code looks ok to me, as long as you have an instance of this script added to each button then I think it would work. Maybe you have another issue with your configurations.
I think there’s a better way of implementing the buttons though anyway. Rewrite your script to something like:
public class MainMenu : MonoBehaviour {
public void Start()
{
Application.loadLevel(1);
}
public void Quit()
{
Application.Quit();
}
}
Add this script to the MainMenu canvas, or some object that is a parent of the buttons. Then add an OnClick event to each button in the inspector. Just select the button in the scene and look for the event dropdown belonging to the Button component in the inspector. And point the event for each button to the correct method in this script.
Thanks man. To get the code to work I changed “Start” and “Quit” to “ifStart” and “ifQuit”. I also went to the button inspectors and on the “On Click” I added the code object to it and then labeled the buttons “ifStart” and “ifQuit” from the drop down of that section. Works fine now.