Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperHB · Nov 11, 2014 at 05:23 AM · guipause menugui.buttonmainmenu

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
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richyrich · Nov 11, 2014 at 05:48 AM 2
Share

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.

avatar image Kerihobo · Nov 11, 2014 at 05:55 AM 0
Share

I wanna help you, but i don't wanna read through all that code, can you just post the problem area?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Nov 11, 2014 at 06:17 AM 0
Share

DontDestroyOnLoad is normally used in the singleton pattern or with a preloader.

avatar image SuperHB · Nov 11, 2014 at 03:39 PM 0
Share

Okay I made a Singleton Pattern for it and it worked. Thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to go to a new gui.window by clicking a gui.button? 1 Answer

Hold Down GUIButton? 1 Answer

GUI texture updating in preview but not in game 1 Answer

How to make GUI buttons react when pressed?[C#] 2 Answers

Need help using GUI.Button/ input storage 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges