Restart Buttons are easy and often overlooked!

Restart

I have the chance to play a large number of prototype games.

These can be work-in-progress, concepts, level testing, and the like.

I have a pet peeve.

The single most overlooked feature is a simple “restart” button.

 

These are easy to code, simple to implement and often never included.

Do it. Don’t avoid it.

Here is some simple code showing FOUR different ways to do it if you don’t want to look it up:

http://pastebin.com/xbfx9WaV

Or pasted here in the post just in case:

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

public class RestartNow : MonoBehaviour {
  void OnGUI () {
    if (GUILayout.Button ("Restart")) {
      Application.LoadLevel(Application.loadedLevel);
    }
    if (GUI.Button (new Rect (10,30,75,20), "Restart")) {
      Application.LoadLevel(Application.loadedLevel);
    }
  }

  void Update () {
    if (Input.GetKey ("r")) {
    Application.LoadLevel(Application.loadedLevel);
    }
    //Requires this button to be set in the Input Manager
    if (Input.GetButtonDown ("Restart")) {
      Application.LoadLevel(Application.loadedLevel);
    }
  }
}

or in Unity’s JavaScript:
function OnGUI () {
  if (GUILayout.Button ("Restart")) {
    Application.LoadLevel(Application.loadedLevel);
  }

  if (GUI.Button (new Rect (10,30,75,20), "Restart")) {
    Application.LoadLevel(Application.loadedLevel);
  }
}

function Update () {
  if (Input.GetKey ("r")) {
    Application.LoadLevel(Application.loadedLevel);
  }

  if (Input.GetButtonDown ("Restart")) {
    Application.LoadLevel(Application.loadedLevel);
  }
}

http://unity3d.com/support/documentation/Manual/Input.html http://unity3d.com/support/documentation/Components/class-InputManager.html

No Comments

Leave a Comment

Please be polite. We appreciate that. Your email address will not be published and the required fields are marked.