Scripting Project: A Simple 3-Scene Selection Menu

Introduction

For this project, I have created a simple 3-scene selection menu that modifies the transform properties of game objects based on scene selection. There are also buttons to return to the selection menu, and a countdown timer to return to the selection menu. The scenes are:

  • Scene 0: Initial Load Scene
  • Scene 1: Main Menu Scene
  • Scene 2: Day Scene
  • Scene 3: Night Scene
  • Scene 4: Fast Day/Night Scene

Scene 0: Initial Load Screen

I decided to start with an initial load scene (Scene 0). Scene 0 loads the camera and the player game objects. The reason that I decided to load these game objects in a separate scene is that the player should be able to control the player game object throughout the entire game. By including these two game objects in an initial scene, the game does not have to render new game objects at every scene load. There is a script attached to these two game objects that ensures that they are not destroyed when a new scene is loaded.

using UnityEngine;
using System.Collections;

public class DontDestroy : MonoBehaviour {

    void Awake () {
        DontDestroyOnLoad(gameObject);
    }

}

Scenes 1 to 4 contain a script to modify the transform of the player game object, defined by the public Transform variable startPos. This script is attached to a game object and the startPos variable is set in the Unity inspector. That way, the player’s start position can be changed simply by moving a game object in the scene window.

using UnityEngine;
using System.Collections;

public class PlayerStartPosition : MonoBehaviour {
    private GameObject player;
    public Transform startPos;

    void Awake () {
        player = GameObject.Find("Player");
        player.transform.position = new Vector3(startPos.transform.position.x, startPos.transform.position.y);
    }

}

Every scene has a game object to handle scripts. Attached to this game object is a script to load scenes. The script handles the amount of time to change to the main menu scene, the countdown text, and loading the main menu. The time to next scene and countdown time is set in the Unity inspector. There is a public bool that controls whether or not the countdown text is displayed in the scene. In the initial scene this is set to false and the countdown value is set to 0. The player does not notice that this scene is loaded at all.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LoadSceneAfterTime : MonoBehaviour {

    public float timeToSceneChange;
    public string sceneName;
    public bool displayCountdownText;
    public Text text; 

    void Start () {
        text.enabled = false;
    }

    void Update () {
        if (timeToSceneChange > 0) {
            timeToSceneChange -= Time.deltaTime;

            if (displayCountdownText) {
                text.enabled = true;
                text.text = "Menu return in " + Mathf.Round (timeToSceneChange) + " seconds";
            } else {
                text.enabled = false;
            }

        } else {
            Application.LoadLevel (sceneName);
        }

    }
}

Scene 1: Main Menu

This is a simple scene where the player game object is placed between 4 walls. The player can move around to get used to the controls of the game. This scene contains UI elements such as a canvas and buttons to navigate between scenes or quit the game.

A script is attached to the canvas game object that handles a method for changing scenes upon input of the player (i.e. on button click).

using UnityEngine;
using System.Collections;

public class LoadOnClick : MonoBehaviour {

    public void LoadScene (int level) {
        Application.LoadLevel(level); 

    }
}

When the button is pressed, this method is called. The level is set in the button’s inspector in Unity. The variable that corresponds to each scene is set in the build settings of the game.

The canvas also handles a quit script. This method quitGame() is called when the player clicks on the quit button.

using UnityEngine;
using System.Collections;

public class ClickToQuit : MonoBehaviour {

    public void quitGame () {
         //UnityEditor.EditorApplication.isPlaying = false;
         Application.Quit();
    }

}

Scenes 2-4

Scenes 2-4 are essentially variations of the same scene. Each scene modifies the player’s transform by defining a different startPos. In other words, the player starts at a different point in each scene. Each scene has a countdown until the menu selection scene is loaded again and a button to receive user input to return to the menu selection scene. This is the same script that was used in scene 0, except this time the bool displayCountdownText is set to true in the Unity inspector. The same LoadScene(int level) method handles returning to the main scene. In other words, the button handles player input, and the player can either return to the main menu by pressing the button or waiting until the countdown reaches 0.

Conclusion

I also learned how to additively add components from a scene to a current scene using a script, although I did not use this in my game. This script allows game objects that exist in another scene to be added to the current scene.

using UnityEngine;
using System.Collections;

public class LoadAdditive : MonoBehaviour {

    public void LoadAddOnClick (int level){
        Application.LoadLevelAdditive(level); 
    }
}

This may be useful in the future for adding a certain number of enemies in a scene. For example, I could add a slider bar to a menu selection to define how many enemies will be in the scene and store that number in a counter variable. When the player clicks a button to load a scene, a for loop will run to load a scene the number of times in the counter variable. The scene that is loaded will only contain an enemy game object.

Switching between scenes using UI elements such as buttons is important. I could use a similar script that could change the scene upon player collision with a game object. This could be set up in a way that if I pass through a door, a new scene is loaded and the player game object is transported to the position of a door in the new scene.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s