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 Velketor · Jun 11, 2012 at 09:40 PM · destroyloadlevelgame objectself

Destroy vs Don't Destroy

My gameObject has a script on it. The script prevents my gameObject from being destroyed when a new level is loaded.

However, I have one specific level where I'd like the gameObject to be destroyed upon loading the level.

How can I force the gameObject to destroy itself, when I'm also forcing the same gameObject NOT to destroy itself?

Please help if you can. Thank you.

Comment
Add comment
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

2 Replies

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

Answer by You! · Jun 11, 2012 at 11:43 PM

Don't you love conditional statements?

What you need to do is put a little extra code in the script you have the function "Application.LoadLevel()" Firstly, you need a boolean at the beginning of the script (like @multinfs has). Then, you need this after the Application.LoadLevel() or just before that function:

 var toDestroy : GameObject = GameObject.Find(/*name of object here*/);

 if(destroyOnLoad){
     Destroy(toDestroy.gameObject);
 }



In the end, it should look somewhat like this:

 var destroyOnLoad : boolean = false;
 
 function WhateverThisFunctionActuallyIsInYourGame (){
 var toDestroy : GameObject = GameObject.Find("prefabPortal");
 if(destroyOnLoad){
     Destroy(toDestroygameObject);
 }
 Application.LoadLevel(/*whatever level you have here in quotes*/);
 }
Comment
Add comment · Show 5 · 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 Velketor · Jun 12, 2012 at 12:19 AM 0
Share

thank you for helping me. I tried to combine both scripts but I failed when I inserted the name of my object. It says "$$anonymous$$ identifier" when I insert the word Portal.

var destroyOnLoad : boolean = false; var toDestroy : GameObject = GameObject.Find(prefabPortal);

function Start() {

if(destroyOnLoad) Destroy(gameObject);

}

if(destroyOnLoad){ Destroy(toDestroy.gameObject); }

avatar image Bunny83 · Jun 12, 2012 at 12:29 AM 1
Share

You've heard about strings before, right? An object name is not a variable in the script. The name itself should be a string literal which is defined by putting your text between quotation marks"".

 GameObject.Find("prefabPortal");
avatar image You! · Jun 12, 2012 at 12:30 AM 1
Share

Firstly, don't actually put the if statement outside of a function. Secondly, do not put the code inside of the start function. Wherever you switch the levels is where you need to put the code. It should look somewhat like this:

 var destroyOnLoad : boolean = false;

 function WhateverThisFunctionActuallyIsInYourGame (){
 var toDestroy : GameObject = GameObject.Find("prefabPortal");
 if(destroyOnLoad){
     Destroy(toDestroygameObject);
 }
 Application.LoadLevel(/*whatever level you have here in quotes*/);
 }

And don't forget to do what @Bunny83 said.

I'll put this code in the answer.

avatar image Velketor · Jun 12, 2012 at 02:35 AM 0
Share

Worked perfectly. Thank you so much for explaining everything. I am still trying to learn how to program so this is very helpful. I appreciate it!

avatar image Timurov · Nov 14, 2012 at 07:52 AM 0
Share

do you have the extra code in c#?

avatar image
0

Answer by multinfs · Jun 11, 2012 at 09:50 PM

Well, if I get it right you want a object to be toggleable to be destroyed or not when a level is loaded, please correct me if I'm wrong. Now this could be made in a few ways but I think this would be a easy way:

First you create a prefab if you havent already of the object/objects, and then u just add a script with this:

 var destroyOnLoad : boolean = false;
 
 function Start() {
 
  if(destroyOnLoad)
   Destroy(gameObject);
 
 }

I think it could be function Awake() too but I'm not really sure whats the difference :P

Hope it helps :)

Comment
Add comment · Show 1 · 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 Velketor · Jun 11, 2012 at 10:57 PM 0
Share

thank you for the script. Unfortunately it's still not going away. I'm thinking I'll make an empty level between my real levels and destroy all objects when they enter the scene and then send it to the next level, if that makes sense.

thanks again!

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

Application.LoadLevel not loading levels 1 Answer

Destroying game object next to player 1 Answer

Can I put a Level as a property of a GameObject? 1 Answer

Post-Build Custom Level Loading 2 Answers

level loader script wont work 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