- Home /
How to make a simple menu button with C Sharp
Hi, I'm very new to unity and coding and I have to make a simple game for my uni course.
All I want is a to make a button on the main menu that when clicked will load the level.
I've made the menu screen with a "Start" text (I think I made it a 3D text, not GUI) and this is the script I've got so far that I've managed to jumble together from looking at other online sources but it doesn't work:
using UnityEngine;
using System.Collections;
public class ButtonScript: MonoBehaviour
{
public bool isQuit = false;
void OnMouseDown ()
{
if(isQuit)
{
Application.Quit ();
}
else
{
Application.LoadLevel ("flyinglevel");
}
}
}
Any help would be greatly appreciated
Thanks
Chris
Answer by Stormizin · Dec 27, 2013 at 01:46 PM
Hello Chris and welcome to the community, you can simply use this code:
void OnGUI(){
//Here if the player hit the button Start will trigger an action
if(GUI.Button(new Rect(Screen.width/2, Screen.height/2, 100, 25), "Start Game!")){
//Here we call the method who will call the level named "LevelName"
Application.LoadLevel("LevelName");
}
}
Also See: GUI.Button and Application
Give it a shot! Good Luck!
Thanks, it worked. But how do I change the font and size of that newly created gui?
You can easily create your own UI within a GUI.Skin, see GUI.Skin
If helped and solved your problem please mark as correct to keep the community organized (:
Your answer
Follow this Question
Related Questions
Animate GUI Elements 1 Answer
Cube click pop-up menu 0 Answers
How do I create a Gmod item spawn menu in unity? 0 Answers