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 OctoMan · Jun 24, 2015 at 01:03 AM · gameobjectdestroyif statementonce

Only delete the gameobject ONCE

I want to destroy OBJ001 ONCE it's entered the 2000exp, how could i fix my script part to get it to that?

 if(GM.enemy_exp >= 2000)
         {
 
             if(BuildSpots[0].tag == "TurretSpotClosed")
             {
                 Destroy(OBJ001);//destroy the object only once
                 BuildSpots[0].tag = "TurretSpotOpen";
                 OBJ001 = Instantiate(Turrets[1], BuildSpots[0].position, Quaternion.identity)as GameObject;
                 BuildSpots[0].tag = "TurretSpotClosed";
             }
         }
         else if(GM.enemy_exp >= 600)
         {
             if(BuildSpots[0].tag == "TurretSpotOpen")
             {
                 OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
                 BuildSpots[0].tag = "TurretSpotClosed";
             }
         }
Comment
Add comment · Show 3
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 revolute · Jun 24, 2015 at 01:40 AM 0
Share

You can just make a bool variable and set it to false. Then, check if your exp is above 2000 and that bool is false. Set it to true and destroy it.

Easiest way out, at least in my opinion.

avatar image maccabbe · Jun 24, 2015 at 03:48 AM 0
Share

You could also start a coroutine that checks if Gm.enemy_exp reaches 2000. If it does, then destroy the object and end the coroutine.

Or you could control how G$$anonymous$$.enemy_exp is increased. Then if G$$anonymous$$.enemy_exp<2000 but G$$anonymous$$.enemy_exp+exp>=2000 set G$$anonymous$$.enemy_exp+=exp and destroy the gameObject.

avatar image OctoMan · Jun 24, 2015 at 07:55 AM 0
Share

The problem is, i need to do that several time, so bool is not working i think.

Starting a coroutine is the same problem, i need to do this several times. No option for me i think. Still thanks.

2 Replies

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

Answer by Mark Gossage · Jun 24, 2015 at 03:48 AM

You might be able to use the name/tag of the OBJ001 to solve it. Otherwise, you will be creating/destroying objects like mad. I like revolute's idea of a bool, but since it seems that you have several levels, I would recommend an enum. I'm assuming this is some kind of tech-level advancement for an RTS or similar. So this is how I would approach it:

 enum Tech{Level0,Level1,Level2};
 Tech currentTech=Tech.Level0;
 GameObject OBJ001=null;
 
 ...
 
 if (currentTech==Tech.Level0 && GM.enemy_exp >= 600)
 {
   // create OBJ001
   currentTech=Tech.Level1;
 }
 if (currentTech==Tech.Level1 && GM.enemy_exp >= 2000)
 {
   // delete old OBJ001
   // create new OBJ001
   currentTech=Tech.Level2;
 }
 // and so on.


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 OctoMan · Jun 24, 2015 at 07:59 AM 0
Share

This seems to be very solid. I'll give it a try, and give you feedback.

If i have 3 techlevels per "tier", can i use another enum to increase that tier too, and begin again at tech level 1? This would maybe safe me alot of coding. Or Should i just take an int and increase that for the "tier"?

avatar image OctoMan · Jun 24, 2015 at 12:14 PM 0
Share

Works pretty well!

 if(currentTech == Tech.Level0 && G$$anonymous$$.enemy_exp>= 600)
         {
             OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
             currentTech = Tech.Level1;
         }
         if(currentTech == Tech.Level1 && G$$anonymous$$.enemy_exp>= 2000)
         {
             Destroy(OBJ001);
             OBJ001 = Instantiate(Turrets[1], BuildSpots[0].position, Quaternion.identity)as GameObject;
             currentTech = Tech.Level2;
         }
 
         if(currentTech == Tech.Level2 && G$$anonymous$$.enemy_exp>= 3200)
         {
             Destroy(OBJ001);
             OBJ001 = Instantiate(Turrets[2], BuildSpots[0].position, Quaternion.identity)as GameObject;
             currentTech = Tech.Level3;
         }

I just need to do this alot of times, Also i have 4 BuildSpots total. Shall i use 4 funktions to handle every spot seperatly?

avatar image
0

Answer by Ibzy · Jun 24, 2015 at 07:49 AM

Is the above code running in the Update() function? If so, why not put it in the same function that adds the exp (or call it from that function)? This way it will only check for >2000 when it has added to the variable.

Comment
Add comment · Show 9 · 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 OctoMan · Jun 24, 2015 at 08:00 AM 0
Share

Currently, yes. Basicly i need to check the exp for the whole time, or evertime it changes. Those change are at least every 2 seconds.

avatar image Ibzy · Jun 24, 2015 at 12:16 PM 0
Share

It changes every 2 seconds on a constant ticker, or about every 2 seconds based on an event (e.g. enemy kill)?

avatar image OctoMan · Jun 24, 2015 at 12:22 PM 0
Share

On enemy kills & per timer.

avatar image Ibzy · Jun 24, 2015 at 12:28 PM 0
Share

Think I can see what's happening. once enemy_exp>=2000 you run through the process...and the enemy_exp is not reset, so on every frame you are destroying and instantiating.

Either adjust your enemy_exp so that it starts from zero after an "upgrade" or flag that the upgrade as already happened and check for that flag?

avatar image OctoMan · Jun 24, 2015 at 12:34 PM 0
Share

That's the problem, it will get destroyed and instantiate once per frame, thats the problem. resetting exp is no option. Checking and flagging that over and over again seems not very comfortable, Since i have 3 upgrade possibilitys per 6 Tiers, per 4 Buildspots.

Show more comments

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Problem with timed code-execution (audioplay and object destruction) 1 Answer

Trying to make a simple inventory: 0 Answers

Destruct gameobject by call from another script 1 Answer

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 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