- Home /
GameObject duplicates every time my GUI button is pressed
Okay, so every time I hit my Main Menu Button from my Pause Menu and every time I press it, it duplicates the Main Menu GameObject. Is there a way to stop it from duplicating the GameObject?
Here is my code:
Main Menu JAVASCRIPT:
#pragma strict
//options
private var selected : boolean = false;
public static var showMenu : boolean = false;
public static var isMenu : boolean = false;
//Logo
var logo : Texture;
var logoSize : int = 381;
var logoYAxis : int = 263;
//Dynamic GUI Var
var originalWidth = 1200.0; //Defines the original resolution
var originalHeight = 675.0; //Used to create the GUI Contents
private var scale: Vector3;
function Start()
{
selected = false;
showMenu = true;
}
function Update()
{
}
function OnGUI()
{
scale.x = Screen.width/originalWidth; //Calculates Horizontal Scale
scale.y = Screen.height/originalHeight; //Calculates Vertical Scale
scale.z = 1;
var svMat = GUI.matrix; //Saves current Matrix
//Substitute Matrix - Only scale is altered from standerd
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
//Buttons
if(showMenu == true)
{
//Logo
//GUI.DrawTexture(Rect(10,10,logoSize,logoYAxis),logo,ScaleMode.ScaleToFit,true,0);
if (GUI.Button(Rect(10,300,200,25), "Play"))
{
Application.LoadLevel(1);
Screen.showCursor = false;
Screen.lockCursor = true;
showMenu = false;
}
if (GUI.Button(Rect(10,330,200,25), "Settings"))
{
Settings.showOptions = true;
showMenu = false;
isMenu = true;
Pause.isPauseMenu = false;
}
if (GUI.Button(Rect(10,635,100,25), "Quit"))
{
Application.Quit();
}
if (GUI.Button(Rect(990,635,200,25), "Website"))
{
Application.OpenURL ("http://badmodelgames.net78.net/");
}
}
}
Settings JAVASCRIPT:
#pragma strict
public static var showAudio : boolean = false;
public static var showVid : boolean = false;
public static var showOptions : boolean = false;
var FS : int;
var AA : int;
var AF : int;
var MV : int;
var Test : int;
//Dynamic GUI Var
var originalWidth = 1200.0; //Defines the original resolution
var originalHeight = 675.0; //Used to create the GUI Contents
private var scale: Vector3;
function Start()
{
showAudio = false;
showVid = false;
showOptions = false;
}
function Update()
{
}
function Awake()
{
DontDestroyOnLoad(this);
}
function SaveSettings()
{
/*PlayerPrefs.SetInt("AntiAliasing", AA);
PlayerPrefs.SetInt("AnisotropicFiltering", AF);*/
}
function OnGUI()
{
scale.x = Screen.width/originalWidth; //Calculates Horizontal Scale
scale.y = Screen.height/originalHeight; //Calculates Vertical Scale
scale.z = 1;
var svMat = GUI.matrix; //Saves current Matrix
//Substitute Matrix - Only scale is altered from standerd
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
//Options Menu
if(showOptions == true)
{
//Settings
if (GUI.Button(Rect(10,300,200,25), "Video Settings"))
{
showVid = true;
showOptions = false;
}
if (GUI.Button(Rect(10,330,200,25), "Audio Settings"))
{
showOptions = false;
showAudio = true;
}
if(MainMenu.isMenu == true)
{
//back to main menu
if(GUI.Button(Rect(10,635,100,25),"Back"))
{
showOptions = false;
MainMenu.showMenu = true;
}
}
if(Pause.isPauseMenu == true)
{
//back to main menu
if(GUI.Button(Rect(10,635,100,25),"Back"))
{
showOptions = false;
Pause.pauseMenu = true;
}
}
}
//Video Settings Menu
if(showVid == true)
{
//back to main menu
if(GUI.Button(Rect(10,635,100,25),"Back"))
{
showOptions = true;
showVid = false;
}
//Save Settings
/**if(GUI.Button(Rect(1090,635,100,25),"Save"))
{
showOptions = true;
showVid = false;
}*/
}
//Audio Settings Menu
if(showAudio == true)
{
//back to main menu
if(GUI.Button(Rect(10,635,100,25),"Back"))
{
showOptions = true;
showAudio = false;
}
}
//Restore matrix before returning
GUI.matrix = svMat; //Restore matrix
}
Pause Menu JAVASCRIPT:
#pragma strict
public static var isPaused : boolean = false;
public static var pauseMenu : boolean = false;
public static var isPauseMenu : boolean = false;
//Dynamic GUI Var
var originalWidth = 1200.0; //Defines the original resolution
var originalHeight = 675.0; //Used to create the GUI Contents
private var scale: Vector3;
function Start()
{
isPaused = false;
pauseMenu = false;
Screen.showCursor = false;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.Escape) && isPaused == false)
{
//Pauses Game
isPaused = true;
//Shows Cursor
Screen.showCursor = true;
//Locks Cursor
Screen.lockCursor = false;
//Enables Pause Menu
pauseMenu = true;
//Disable Movement
GameObject.FindGameObjectWithTag("MainCamera").GetComponent(MouseLook).enabled = false;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
GameObject.Find("First Person Controller").GetComponent(CharacterMotor).enabled = false;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = false;
}
else if(Input.GetKeyDown(KeyCode.Escape) && isPaused == true)
{
//Unpauses Game
isPaused = false;
//Hides Cursor
Screen.showCursor = false;
//Locks Cursor
Screen.lockCursor = true;
//Disables Pause Menu
pauseMenu = false;
//Enables Movement
GameObject.FindGameObjectWithTag("MainCamera").GetComponent(MouseLook).enabled = true;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
GameObject.Find("First Person Controller").GetComponent(CharacterMotor).enabled = true;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = true;
}
}
function OnGUI()
{
scale.x = Screen.width/originalWidth; //calculates horizontal scale
scale.y = Screen.height/originalHeight; //calculates vertical scale
scale.z = 1;
var svMat = GUI.matrix; //saves current matrix
//substitute matrix - only scale is altered from standerd
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
//Pause GUI
if(isPaused)
{
GUI.Label(Rect(600,10,100,10000),"Paused");
if(pauseMenu == true)
{
//Buttons:
//X Position,Y Position,X Size,Y Size
if (GUI.Button(Rect(10,300,200,25),"Resume"))
{
isPaused = false;
Screen.showCursor = false;
Screen.lockCursor = true;
//enables movement
GameObject.FindGameObjectWithTag("MainCamera").GetComponent(MouseLook).enabled = true;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
GameObject.Find("First Person Controller").GetComponent(CharacterMotor).enabled = true;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = true;
//Disables any Menus Enabled (To be safe)
pauseMenu = false;
Settings.showOptions = false;
MainMenu.showMenu = false;
}
if (GUI.Button(Rect(10,330,200,25), "Options"))
{
pauseMenu = false;
Settings.showOptions = true;
MainMenu.isMenu = false;
isPauseMenu = true;
}
if (GUI.Button(Rect(10,360,200,25),"Main Menu"))
{
Application.LoadLevel(0);
Screen.showCursor = true;
pauseMenu = false;
Settings.showOptions = false;
MainMenu.showMenu = true;
}
}
}
//restore matrix before returning
GUI.matrix = svMat; //restore matrix
}
In the nicest possible way, this is NOT the way to post code. You have included commented out code, empty functions and blank space. Additionally, you have supplied a LOT of functions that have nothing to do with the problem. You want people to scroll up and down 500 lines of code? You only need to supply the relevant and related code sections, but not scripts in their entirety.
I wanna help you, but i don't wanna read through all that code, can you just post the problem area?
Answer by prof · Nov 11, 2014 at 05:58 AM
You have DontDestroyOnLoad(this); in settings script. So when you are loading mainMenu level, you keeping old object and getting new one. For solution you can use singleton pattern (check if object already created)
DontDestroyOnLoad is normally used in the singleton pattern or with a preloader.
Okay I made a Singleton Pattern for it and it worked. Thanks