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 Mangamaster247 · Sep 19, 2016 at 04:59 PM · programmingif-statementsmultiplefor-loop

Using i in if statement

So I'm doing some raycasting with 5 raycasts and figured since they all looked alike, why don't I use a for loop instead with "i" as a varying factor.

So what I wanna do is something like

void Update(){

      for (int i = 1; i < 6; i++){

             if (Physics.Raycast(T(i), fwd, out hit(i), 5))
             {
                 if (hit(i).transform.tag.Contains("Short"))
                     hitShortBar = true;

                 if (hit(i).transform.tag.Contains("Tall"))
                     hitTallBar = true;
             }
         }

T being the transform I'm using

T1, T2... and so on asweel as hit1, hit2... and so on.

now I know this wouldn't work with the parenthesis around "i" that's just where I want to use "i" :).

Is this possible?

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

3 Replies

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

Answer by ScaniX · Sep 19, 2016 at 05:28 PM

I think the array is not there, yet. I think what he has is T1, T2, T3 as variables and what is wanted is something like this:

 void Update(){
       Transform[] T = { T1, T2, T3, T4, T5, T6 };
       for (int i = 0; i < 6; i++){
              if (Physics.Raycast(T[i], fwd, out hit, 5))
              {
                  if (hit.transform.tag.Contains("Short"))
                      hitShortBar = true;
                  if (hit.transform.tag.Contains("Tall"))
                      hitTallBar = true;
              }
       }
 }

There is no need to to define the array in Update() each time, it is just an example. You can probably just add a public Transform[] T; member to your class and fill it in the inspector.

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

Answer by ClearRoseOfWar · Sep 19, 2016 at 05:05 PM

Im not sure what your question is... But yes what you've put down should work. Will be heavy on processing tho.. maybe put the for loop in a function instead and only call it when you need to.

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

Answer by tanoshimi · Sep 19, 2016 at 05:13 PM

Assuming that T is an array of GameObjects and hit is an array of RaycastHits, then what you've written will nearly work except it requires square bracket syntax to access the index of an array rather than round brackets:

 if (Physics.Raycast(T[i], fwd, out hit[i], 5))
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Question on Possible FOR Loop Condition Conflict 0 Answers

if statement not returning true 1 Answer

Create DoTween from for loop? 1 Answer

If statement within the For-Loop is not working - Puzzled! 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