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 AlucardJay · Jan 26, 2012 at 06:38 PM · gameobjectdestroydontdestroyonload

game object is not being destroyed

Hello all. This is my first question, though I have many ! So .. I have an empty gameObject prefabMusicPlayer, that carries through the scenes playing music. I wish to destroy it on the win/lose screen as the main menu has different audio. During testing this code in the GUI function worked (from the sceneScreenWin/Lose script, not prefabMusicPlayer script):

var, and Find in start function on scriptScreenWin :-

 // prefabMusicPlayer script
 var MusicPlayerScript : scriptMusicPlayer;
 
 // load MusicPlayerScript
 MusicPlayerScript = GameObject.Find("prefabMusicPlayer").GetComponent(scriptMusicPlayer);

from the GUI function on scriptScreenWin :-

 // Back Button    
        GUI.color = Color.green;    
        if (GUI.Button (Rect (10, 170, 180, 30), "Back to Menu"))    
        {    
          // DESTROY prefabMusicPlayer 
          //Destroy (MusicPlayerScript.gameObject);  // problem, added function    
          // wait so object is destroyed
          destroyAndWait ();
 
          // Load sceneScreenMainMenu    
          //Application.LoadLevel ("sceneScreenMainMenu");    
        }

But when I uploaded and ran from the website, the gameObject prefabMusicPlayer was not being destroyed. I added a function :

 function destroyAndWait ()    
 {    
     // DESTROY prefabMusicPlayer    
     //Destroy (MusicPlayerScript.gameObject);
 
     // script from unity answers advice    
     Destroy(scriptMusicPlayer.GetInstance().gameObject);          
 
     // wait so object is destroyed    
     yield WaitForSeconds (0.2);
 
     // Load sceneScreenMainMenu    
     Application.LoadLevel ("sceneScreenMainMenu");    
 }

... with no success. I found this example : http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html ; and added it to the scriptMusicPlayer (ADDITIONAL SCRIPT pt 1 & 2). Again, the gameObject still exists after returning to sceneScreenMainMenu, then sceneLevel1 is adding Another MusicPlayer :

the full scriptMusicPlayer :-

 // Music Player Script  (object carried through levels)
 // -------------------
 
 
 //
 var playerX : float = 35.0;
 var playerY : float = 11.0;
 var playerZ : float = 0.0;
 
 
 // ----    
 
 // ADDITIONAL CODE TO CHECK WHEN prefabMusicPlayer IS NOT DESTROYED - pt1
 
 private static var instance : scriptMusicPlayer; // use the filename of the SCRIPT , not PREFAB
 
 public static function GetInstance() : scriptMusicPlayer 
    {
       return instance;
    }
 
 // ----
 
 //
 function Awake () 
 {
 
     // ADDITIONAL CODE pt2
 
     if (instance != null && instance != this) 
          {
             Destroy(this.gameObject);
             return;
          } 
 
        // END ADDITIONAL CODE pt2     
 
 
     // carry prefabMusicPlayer through scenes
     DontDestroyOnLoad (transform.gameObject);
 }
 
 // ----
 
 //
 function Start () 
 {
     // assign the instance (don't forget)
     instance = this;
 
     // play audio if music is enabled
     if (PlayerPrefs.GetInt("Music") == 1)
     {
         audio.Play();
     }
 }
 
 // ----
 
 //
 function FixedUpdate () 
 {
     // move Music Player to audio listener 
     transform.position = Vector3(playerX, playerY, playerZ);
 
     //check if music is toggled to OFF
     if (PlayerPrefs.GetInt("Music") == 0)
     {
         // mute audio
         audio.mute = true;
     }
     else
     {
         // un-mute audio
         audio.mute = false;
     }
 }
 
 // ----

Any help would be greatly appreciated, as this would fix one of the last problems in my first game built with Unity ! (here's the project so far : http://www.alucardj.net16.net/WebPlayer.html ). Thanks, Jay.

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 Berenger · Jan 27, 2012 at 01:16 AM 1
Share

I don't know about Destroy and Load issues, but you should change the UV of the cubes falling down in the menu so the sides are transparent, and use "Clamp" ins$$anonymous$$d of "Repeat" in the texture parameters, to avoid lines on the edges of your bubbles.

avatar image AlucardJay · Jan 27, 2012 at 02:04 AM 0
Share

thanks, I shall check it out , as it was bugging me too :)

1 Reply

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

Answer by rabbitfang · Jan 27, 2012 at 01:36 AM

The reason that it isn't being destroyed is that MusicPlayerScript is referencing the class, not a specific script object.

In your second script instance is not even being assigned.

In your Start() function of your scriptMusicPlayer, add instance = this;

Before you load the new scene, call Destroy(scriptMusicPlayer.GetInstance().gameObject);, which should remove the prefab.

Btw, your naming scheme is VERY confusing (got mixed up between MusicPlayerScript and scriptMusicPlayer)

Comment
Add comment · Show 4 · 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 AlucardJay · Jan 28, 2012 at 03:36 AM 0
Share

thankyou for the informative reply. you explained the point of class and object, which I shall check out more. I just started unity, with a little experience in flash (click homepage button on my game).

But i just had an afterthought after reading back on the other link; should I add :- else { instance = this; } before the destroy (i also see transform.gameObject is also this.gameObject).

as the static var and static function wasn't $$anonymous$$e, I had a little trouble understanding it, and missed the this statement.

i agree my na$$anonymous$$g is confusing on anything i put in Find. It started when I was learning what var belonged to what name. All my hierarchy is prefabDooDaa , scriptDooDaa and matDooDaa. I shall make the var name DooDaa_Script = scriptDooDaa in future :) many thanks

avatar image AlucardJay · Jan 28, 2012 at 03:39 AM 0
Share

I have just applied your advice, and in the program i checked the hierarchy and the music player was NOT destroyed when going back to the main menu. I checked and the scripts are identical to the scripts on this page.

the problem is still there after testing a few times , so now I am very confused . and i don't want to cheat and do something bad like have a scene Start that is a duplicate of $$anonymous$$ain $$anonymous$$enu, create the $$anonymous$$usicPlayer in Start, then have Win/Lose go to $$anonymous$$ain $$anonymous$$enu where no $$anonymous$$usic Player is created. I didn't know what to do, so I have revised my original question (self answer removed, sry).

avatar image AlucardJay · Jan 28, 2012 at 07:01 AM 0
Share

just tested a few times. the reason i thought it was working at first is on the first round from losing to starting again, the music player is not heard. but play again and lose again, and the error can be heard on 2nd return to the main menu.

avatar image AlucardJay · Apr 15, 2012 at 08:43 AM 0
Share

@rabbitfang , Sorry it's taken so long for me to get back to this. When I first made this game and posted this question, I didn't have an understanding of classes or types(static, public, private). Now looking back on this project to fix the problem, I get it.

Thankyou for your answer, here's some long-overdue well earned karma =]

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Persistent GameObject Between scene Without Serilization 1 Answer

DontDestroyOnLoad for a specific scene 1 Answer

Destroy Object when scene loads 1 Answer

DontDestroyOnLoad Slowing Loading Down 0 Answers


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