- Home /
Main Menu scrpit not working
I'm trying to script my main menu for my game Here is the script
using UnityEngine; using System.Collections;
//extend MonoBehaviour so that we can attach this script to an object public class MenuObject: MonoBehaviour { //A public bool so that we can change in the editor whether or not the button will quit the application public bool isQuit = false;
     //For the next three functions, the collider is absolutely necessary
     //these functions will not fire off if there is no collider
     //Fires off when the mouse hovers over the collider
     //When the mouse is over the item, change the colour of it to 
     //red so that the player knows that it is interacting with it
     void OnMouseEnter()
      {
              renderer.material.color = Color.red;
      }
 
     //Fires off when the mouse leaves the object
     //We want to change the colour of the object back to it's original when the mouse 
     //is no longer over it so that is exactly what we do here
     void OnMouseExit()
      {
              renderer.material.color = Color.white;
      }
     //Fires off when the mouse is clicked while hovering over the object
     //Here we check if the bool was set to true or not and we load the level id not
     //or quit the application if true
     void OnMouseDown()
      {
             if(isQuit)
              {
                     Application.Quit();
              }
             else
              {
                     Application.LoadLevel(1);
              }
      }
}
}
I tried out your script on a default cube. I found that the methods are working as expected, however Application.Quit() doesn't run in the editor, according to the documentation. (I also hadn't defined a level "1", so that part threw an error in the console)
Answer by umangindianic · Jul 02, 2013 at 07:20 AM
Initialize the value of bool in the start function instead of variable. So, you can get exact result you want to do.
 void Start(){
    isQuit = false;
 }
 
 void OnMouseEnter(){
   renderer.material.color = Color.red;
 }
 
 void OnMouseExit(){
   renderer.material.color = Color.white;
 }
 
 void OnMouseDown(){
 
   if(isQuit){
     
     Application.Quit();
   } else {
     
     Application.LoadLevel(1);
   }
 }
Your answer
 
 
             Follow this Question
Related Questions
Menu script not showing up like normal 1 Answer
GuiButton Going To Next Level 0 Answers
need help with script 1 Answer
Gravity switch not working 0 Answers
Making the 'transform.rotation' of an object the 'transform.rotation' of another object 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                