Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bmakloski · Jul 11, 2018 at 04:27 PM · scripting problemgeneral programming

Using For Loop index as if statement parameter within the loop

Ok, I am so confused and my brain just might be at the end of its rope. But I have this for loop :

         for(int i = 1; i <STDTimeFiller.Count;i++)
         {
             TimeSpan loopAdd;
             GameObject looper = STDTimeFiller[i];
             TimeSpan.TryParse(looper.GetComponent<Text>().text, out loopAdd);
             projectedDuration = projectedDuration.Add(loopAdd);
             if (FindCorrectBay.GetComponent<Logger>().PassedProcessNum == "A1" && i==1)
             {
                 ActualTimeObjects[i].GetComponent<Text>().text = FindCorrectBay.GetComponent<Logger>().starter.Add(projectedDuration).ToString("M-dd HH:mm t");
             } 
         }

And I am basically trying to say that if the current task is A1 and we are on the first iteration of the for loop do my stuff. Luckily, without the i call, this round of iteration will work ok and if you take out i==1 it runs as expected. However, when I need to change what happens at A2, this will not work at all. So basically, I just really need the index to be recognized by my if statement.

Can anyone tell me why it's not being recognized? With the i == 1 it completely skips over the if check and continues right down to the next action.

Thank you!

Comment
Add comment · Show 4
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 JVene · Jul 11, 2018 at 05:59 PM 0
Share

While off topic for the question, I'm motivated first to point out that arrays start at index of zero, not 1. Since your loop is based on the count of STDTimeFiller.Count, then unless there is some reason you intend to skip element zero, you're missing 1. The loop will not run "Count" times, it will run "Count-1" times. The first entry is zero, so your first pass is zero, not 1. That aside, I must say that confusion stems from your description. I'm not exactly sure what you need from your text, but I'm trying to understand. As fashioned, the "if" content will only fire when "PassedProcessNum" is AT, and i is 1, and under no other circumstance. Is it possible what you're seeing is related to my opening point, and process A1 is only in effect when i is zero ins$$anonymous$$d of 1? There are loosely related possibilities here, too. What if FindCorrectBay doesn't actually have a "Logger", and that GetComponent returns a null? That's not accounted for here. I'd suggest peeling these combined steps from each other to see what's interfering, because from the code no one can really say without having a debugger show us what's under the hood. So, start with something like:

  Logger l = FindCorrectBay.GetComponent();
  if ( l != null) {...}

This will help show if my suspicion has any relevance. Your "if" will be inside the brackets, executing only if l is not null. That if could be peeled apart, so it's in two stages, to see how these separate tests are interacting:

          if (FindCorrectBay.GetComponent<Logger>().PassedProcessNum == "A1")
          {
             if ( i == 1 ) // might need to be zero?
               {
              ActualTimeObjects[i].GetComponent<Text>().text = FindCorrectBay.GetComponent<Logger>().starter.Add(projectedDuration).ToString("$$anonymous$$-dd HH:mm t");
               }
          } 

See if that moves you toward figuring this out.

avatar image bmakloski JVene · Jul 11, 2018 at 06:02 PM 0
Share

Hey there @JVene ! The reason for skipping index 0 is because STDTimeFiller contains the children of a gameObject where index 0 is the parent and I do not want to change the text of the parent in this case. 1 represents the first child object here. Also, I had also tested with the index being the only parameter and it still wasn't recognizing the index value.

avatar image JVene bmakloski · Jul 11, 2018 at 06:49 PM 0
Share

Did you put the check for the index INSIDE the check for "A1" as in the example I posted?

avatar image JedBeryll · Jul 11, 2018 at 06:48 PM 1
Share

I recommend you print the values of "i" and "FindCorrectBay.GetComponent().PassedProcessNum" before the if statement. $$anonymous$$ost likely they are not both true at the same time.

0 Replies

· Add your reply
  • Sort: 

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

153 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

Related Questions

error Int to string?Assets/Scripts/MyConnect.cs(61,60): error CS0029: Cannot implicitly convert type `int' to `string' 1 Answer

How can i limit object dragging area in my inventory? 0 Answers

Door open script opens all the doors in the scene 3 Answers

Make an object a certain distance from me. 2 Answers

iterate through two lists element by element 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