Jump to content
  • Advertisement
KnowItAllGames

Need a script for Main Menu buttons

This topic is 395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a script for a Play and a Quit buttons i already have the buttons set up i just need some script :)(In C#)

Edited by KnowItAllGames

Share this post


Link to post
Share on other sites
Advertisement

So, as I've created some games already in Unity, I will describe simple example of how I'd do it in Unity these days (and most likely will do it next time I'll use Unity for something):

Here is a simple example I put together in few minutes: https://otte.cz/random/MainMenu.rar

Short description: What you need is some GameObject in scene which will have script containing public functions that are handlers for buttons. The class holding these must derive from MonoBehaviour, like the one in the archive:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenuController : MonoBehaviour
{
    // Function called for button, must have 0 or 1 string argument at most, must be public
    public void NewGameHandler()
    {
        // Load another scene with "Game" name, scenes must be added in Build Settings!
        SceneManager.LoadScene("Game");
    }

    // Function called for button, must have 0 or 1 string argument at most, must be public
    public void QuitHandler()
    {
        // Exit the application
        Application.Quit();
    }
}

Now on the buttons, add a On Click record, where you attach (drag & drop, or just click + select) this game object with above-like MonoBehaviour attached. Next, select (from event drop down) the MonoBehaviour class name and under it there will be a function (NewGameHandler, QuitHandler)... note that the drop down has 2 levels, it is easy to miss it.

It is setup in the project in the archive, so feel free to use anything from it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!