Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 xxluky · Aug 01, 2015 at 10:55 AM · 2darrayfindgameobjectswithtag

How to cut off an array of gameobjects?

Hello, I created a script which changes behavior of a camera in 2D. The behavior is changed by other script attached to triggers in the scene simply by setting bool. Then in my camera script I want to find all the triggers with tag and deside if the bool in other script is true then do something. Everything works fine, but only with the first find component. Always changes behavior the first trigger but not the others. I thing the problem is in my script bellow when working with an array. Any help please?

 private ChangeCameraBehavior changeCameraBehavior;
 
 void Awake ()
     {
 
         GameObject[] changeCamBehaviorCubes = GameObject.FindGameObjectsWithTag ("ChangeBehavior");
         for (int i = 0; i < changeCamBehaviorCubes.Length; i++) {
             changeCameraBehavior = changeCamBehaviorCubes[i].GetComponent<ChangeCameraBehavior>();
         }
     }
 
     void Update ()
     {
         if(changeCameraBehavior.breakPattern == true)
         {
                    //do something
         }
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 Cherno · Aug 01, 2015 at 11:14 AM

Inside your loop, you assign the current ChangeCameraBehavior component to the changeCameraBehavior variable. This means that if the loop runs once in Awake(), this variable will get overwritten during the loop until only the last component is the one that is stored by it. Then, when Update() is called, the variable is accessed, but since it only holds the last component that was assigned to it, only that component will be affected.

You could just "do something" directly in the loop:

 void Awake ()
      {
  
          GameObject[] changeCamBehaviorCubes = GameObject.FindGameObjectsWithTag ("ChangeBehavior");
          for (int i = 0; i < changeCamBehaviorCubes.Length; i++) {
              ChangeCameraBehavior changeCameraBehavior_current = changeCamBehaviorCubes[i].GetComponent<ChangeCameraBehavior>();
              if(changeCameraBehavior_current .breakPattern == true)
              {
                     //do something
              }
          }
      }

Or, if you need to hold references to all the tagged GameObjects, declare the changeCamBehaviorCubes array in the script's main body so it can be accessed by all functions.

 private GameObject[] changeCamBehaviorCubes;
  
  void Awake ()
  {
      changeCamBehaviorCubes = GameObject.FindGameObjectsWithTag ("ChangeBehavior");
  }
  
      void Update ()
      {   
          if(changeCamBehaviorCubes  != null) 
          {
               for (int i = 0; i < changeCamBehaviorCubes.Length; i++) {
               ChangeCameraBehavior changeCameraBehavior_current = changeCamBehaviorCubes[i].GetComponent<ChangeCameraBehavior>();
               if(changeCameraBehavior_current .breakPattern == true)
               {
                     //do something
               }
          }
      }
 



Comment
Add comment · Show 1 · 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 xxluky · Aug 01, 2015 at 11:40 AM 0
Share

Hey, it's working great! Thank a lot.

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

23 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

Related Questions

How to remove gaps in a 2D map. 0 Answers

Array of Tilemaps misbehaving 1 Answer

Getting a 2d Sprite to move over time to an Array 1 Answer

Best way to concat builtin arrays? 2 Answers

Find if no objects with tag exist 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