Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mike_El · May 09, 2018 at 09:55 AM · 2ddestroyobjectsfor-loopif statement

Objects stop being Destroyed. No idea why!

This is a weird issue, and the question is the closest I can get to explaining it

Basically my project just seems to work for 3 layers of objects and when it reaches the 4th layer it won't do anything. This is what I mean: https://youtu.be/BbaXvePqYDc In the video you will see that I dig down 3 layers and can remove the objects "dirt" on it's side but when I go to the fourth layer it does nothing.

I've narrowed the issue to 1 line of code but that same line works for everything before it.

This is the relevant code:

Determines at what y coord the player is

 void Update () {
         if (this.transform.position.y == -10.54467f) {
             minerLevel = "DirtLvl1";
         }
         else if(this.transform.position.y == -12.46467f) {
             minerLevel = "DirtLvl2";
         }
         else if(this.transform.position.y == -14.38467f) {
             minerLevel = "DirtLvl3";
         }
         else if(this.transform.position.y == -16.30467f) {
             minerLevel = "DirtLvl4";
         }
         else if(this.transform.position.y == -18.22467f) {
             minerLevel = "DirtLvl5";
         }
         else if(this.transform.position.y == -20.14467f) {
             minerLevel = "DirtLvl6";
         }
         else if(this.transform.position.y == -22.06467f) {
             minerLevel = "DirtLvl7";
         }
         else if(this.transform.position.y == -23.98467f) {
             minerLevel = "DirtLvl8";
         }
         else if(this.transform.position.y == -25.90467f) {
             minerLevel = "DirtLvl9";
         }
         else if(this.transform.position.y == -27.82467f) {
             minerLevel = "DirtLvl10";
         }
 
     }

This determines if the player is within the correct parameters and if so allowes the object to be destroyed using S (down) and A (left).

 void OnCollisionStay2D(Collision2D collStay)
     {
         if (Input.GetKey ("s") && triggered == false) {
             countDown -= Time.deltaTime;
             if (countDown <= 0) {
                 for (int i = 0; i < 8; i++) {
                     if (collStay.transform.tag == "DirtLvl" + i) {
                         Destroy (collStay.gameObject);
                         countDown = 2.0f;
                     }
                 }
             }
         }
 
         if (entered == true) {
             if (Input.GetKey ("a")) {
                 countDown -= Time.deltaTime;
                 if (countDown <= 0) {
                     for (int i = 0; i < 8; i++) {
                         if (minerLevel == "DirtLvl" + i) {
                             if (collStay.transform.tag == "DirtLvl" + i) {
                                 Destroy (collStay.gameObject);
                                 countDown = 2.0f;
                                 transform.Translate (-0.15f, 0.0f, 0.0f);
                             }
                         }
                     }
                 }
             }
         }
     }

I've found the issue comes in with if (minerLevel == "DirtLvl" + i)but I don't see how that makes sense because it working for the first 3 elevations but nothing further than that.

If there's anymore info needed please let me know. I've already completely rewritten the before because of a similar issue and I don't want to do it again.

Thanks for any help

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
2
Best Answer

Answer by TreyH · May 09, 2018 at 11:55 AM

Do not use exact comparisons on floating-point values.

You're doing two weird things here, so we'll address them both. First, you shouldn't compare floating point values using equality. Since you're doing things apparently in terms of depth, you can probably replace those with a " than" comparison:

 if (this.transform.position.y >= -10.54467f && this.transform.position.y <= -10) {
     minerLevel = "DirtLvl1";
 }
 else if(this.transform.position.y >= -12.46467f) {
     minerLevel = "DirtLvl2"
 }
 // ...


Secondly (and your actual compilation issue), you can't add strings and integers like that. You'll need to convert the index to a string and create a new string with that index appended onto your other one:

 for (int i = 0; i < 8; i++) {
 
     // Need to append the level index to the string
     string iterLevel = "DirtLvl" + i.ToString();
 
     if (minerLevel == iterLevel) {
         if (collStay.transform.tag == iterLevel) {
             Destroy (collStay.gameObject);
             countDown = 2.0f;
             transform.Translate (-0.15f, 0.0f, 0.0f);
         }
     }
 }
Comment
Add comment · Show 3 · 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 Mike_El · May 09, 2018 at 12:44 PM 0
Share

Is there a reason why it works for the first 3 layers but not after that?

avatar image TreyH Mike_El · May 09, 2018 at 08:55 PM 0
Share

How are your levels laid out? Those are some see$$anonymous$$gly random values, is there any reason you chose them?

avatar image Mike_El TreyH · May 09, 2018 at 09:35 PM 0
Share

If you look at the video you see me dig through the layers of dirt, those are the levels. The $$anonymous$$erLevels that you see in the code is based on the y coord of the player when it's on top of the next layer. I used print (this.transform.postion.y); to get the y coord for each level.

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

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

Related Questions

Spawning an object attached to one object, drawn to another. 1 Answer

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

Play Sound on Destroy 1 Answer

Destroying objects one at a time 1 Answer

How to activate an object when another object deactivates? 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