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 fddefdef · Feb 25, 2015 at 09:26 PM · if-statementsif statementdoublesubtractingsubtract

Double subtraction issue

Hi guys,

I'm experiencing something weird. This simple if statement gives me a non-correct output.

         if(lifeTime < 0f)
         {
             GameObject.FindWithTag("GameController").GetComponent<EnemySpawnScript>().enemyOnScreen -= 1;
             Destroy(gameObject);
         }

The subtraction on the variable enemyOnScreen is done two times instead of one. How's this possible if the object is immediately destroyed after the subtraction?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by maccabbe · Feb 25, 2015 at 09:39 PM

This is probably because actual destruction is delayed until the end of the Update loop (after all objects are updated).

http://docs.unity3d.com/ScriptReference/Object.Destroy.html

Comment
Add comment · Show 6 · 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 fddefdef · Feb 25, 2015 at 09:43 PM 0
Share

But i just checked that the additional subtraction is done almost at the beginning of Update loop when lifeTime is still not below zero. How's that possible? I already tried to remove the subtraction line of code and as expected nothing happens. So the problem is that for some reason that line is executed one time even when the if condition is not reached.

avatar image meat5000 ♦ · Feb 25, 2015 at 09:49 PM 0
Share

Just add some other check.

 bool oneShot = false;
 
 if(lifeTime < 0f)
 {
     if(!oneShot)
     {
         oneShot = true;
         GameObject.FindWithTag("GameController").GetComponent<EnemySpawnScript>().enemyOnScreen -= 1;
         Destroy(gameObject);
     }
 }



avatar image fddefdef · Feb 25, 2015 at 09:51 PM 0
Share

Already tried that kind of check. It doesn't work unfortunately.

avatar image meat5000 ♦ · Feb 25, 2015 at 09:51 PM 0
Share

Then you have more than one script at work.

avatar image fddefdef · Feb 25, 2015 at 09:53 PM 0
Share

I checked the entire solution and there is no other place where that variable is subtracted. What else could it be?

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

19 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

Related Questions

I'm having trouble setting a bool. 2 Answers

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

Timer not subtracting... 1 Answer

I'm trying to sort my Npc's into different social classes 1 Answer

Disabling function of a key 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