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 superventure · May 17, 2012 at 05:25 AM · stringcheckelementeach

check if each element in string is true simultaneously?

this script only checks if/when the first item in the string is true. How do you make it check if ALL elements in the string are true?

var blahString:GameObject[];

for(blah in blahString) if (blah.GetComponent(script).on = true) (Unlock goal)

thanks

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

Answer by Eric5h5 · May 17, 2012 at 05:37 AM

That's not a string, that's a GameObject array. You can't do anything literally simultaneously, but you can loop through the array and check each element. If one is false, cancel the loop and don't perform the action. If they are all true, then the loop completes and you can perform the action.

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 superventure · May 17, 2012 at 05:49 AM 0
Share

That's the strange part, I tried cancelling if one was false, but for some reason, it still executes if just the first element is true.

avatar image Eric5h5 · May 17, 2012 at 06:11 AM 0
Share

Then you programmed your logic wrong. Should probably post the code.

avatar image superventure · May 17, 2012 at 06:17 AM 0
Share

var Components : GameObject[];

function Update(){

 for(others in Components)
 if(others.GetComponent("StoryTrig").On == false){ 
     
     return;
     
     
     }else{
     
 print("Completed!");

     
     
 }

}

avatar image superventure · May 17, 2012 at 06:18 AM 0
Share

I did some tinkering, and found out that the 'for each' code will only do a first depth search.

In your suggestion, checking if any are false will work, but saying that the loop will complete else wise is incorrect because it will still return TRUE when just one of the elements becomes true.

When you look at the code, your still basically asking 'if(Components[0] is on/off)', then depending on that boolean, then it will only check the next component in line.

I tried this in the game, and when one component becomes true, it will both print both "completed!" and "off!".

I am trying to get it to check all elements. Is there a way to specifically say "if(Components[ALL].GetComponent("StoryTrig").On == true)" ??

avatar image syclamoth · May 19, 2012 at 03:29 AM 2
Share

Like eric says, you can't actually do anything simultaneously (without very carefully scheduled multithreading, but let's not go there). In this case, it's simply the return condition that needs to be tweaked- you need to make sure that every single one of the components is in a single state.

Now, what you have to realise here, is that that is exactly equivalent to checking for whether one of the objects is in the wrong state! So, all you need to do here is invert your logic:

 for(others in Components)
     if(others.GetComponent(StoryTrig).On){
         return; // One of the objects was active, returning
     }
 }

 // got to the end of the loop without returning:
 print("Completed!");

This way, the only way to get to the last line is if none of the components are active.

In fact, if you simply made the array at the top an array of 'StoryTrig' type, you could avoid the 'GetComponent' call entirely!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I check the name of an element? 2 Answers

How do I check for strings in an array to match up with the index of another array? 1 Answer

Check if two strings are the same 2 Answers

Check array slot against corresponding slot in another array? 1 Answer

Is there a symbol which means 'any character'? 3 Answers


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