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 Quantum · Jun 25, 2011 at 02:46 PM · objectsmultipleaccesscomponents

Accesing components on multiple objects in scene

Hi,

So I instantiated multiple object with all the same attached script component (let's call it MyScript.js).

And I want to access all those objects from another objects script component (let's call this one Control.js).

So is there a way in Unity to access all of those in once?

I tried something similar to this:

 for (var i : int = 0; i < NumberOfObjects; i++) {
     GameObject.FindWithTag("ObjectTagName").GetComponent(MyScript).DoSomething();
 }

But that did't worked out for me. But if I leave the code outside of the for loop, it works for just one Object in the scene.

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 Taugeshtu · Jun 25, 2011 at 02:56 PM

That's what you're trying to do:

  • Run loop with no imaginable limit;

  • Find first gameObject with tag;

  • Access it's component.

That's WRONG logic. What you should to do:

  • Run loop for all FindGameObjectsWithTag;

  • Access gameObject in found array at index;

  • Access it's component

All you need is located in Scripting Reference in GameObject description. Function is called "FindGameObjectsWithTag" or someth. like this.

PS.

By the way - is it that hard, to scroll manual a bit more down?

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 Quantum · Jun 25, 2011 at 03:05 PM 0
Share

Oh yes I see what I'm doing wrong. Thanks, I didn't know about "FindGameObjectsWithTag". But it returns all the gameobjects you're looking for in an array, so I can use this array in a for-loop and access the component on every single gameobject in the array =)

avatar image
2

Answer by ckfinite · Jun 25, 2011 at 03:07 PM

To answer with a code example, try this:

 var objects = GameObject.FindGameObjectsWithTag("ObjectTagName");
 for (var i : int = 0; i < objects.length; i++)
 {
      objects[i].GetComponent(nameOfComponent).DoSomething();
 }
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 Quantum · Jun 25, 2011 at 03:28 PM 0
Share

Thanks =) it works!

avatar image
0

Answer by pavlito · Dec 14, 2015 at 03:53 PM

What I would do is add the objects to a collection as soon as they are instantiated. Then you can easily loop through the collection. You can make the collection add items of your script type, because then you don't need to GetComponent. You can cast it immediately upon instantiate...

     MyType newObj = (MyType)Instantiate(prefab);
     myListOfTypes.Add(newObj);

... provided that the prefab has the component attached, ofcourse :)

     // and loop somewhere
     for (int i = 0; i < myListOfTypes.Count; i++)
     {
          myListOfTypes[i].DoSomething();
     }

Depending on when and where you do FindGameObjectsWithTag(), it can have an impact on your performance. If however, you cannot use permanent collections (due to memory restrictions) then, by all means use FindGameObjectsWithTag once, map to array and loop through that.

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

[SOLVED] Save System with Serialize multiple objects 2 Answers

How to change the color of all objects that use the same material? 0 Answers

Instantiate an array of prefabs? 0 Answers

Is this accurate? Scenes are composed of objects are composed of components are composed of assets 1 Answer

Lag on Collision from Projectile 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