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 $$anonymous$$ · May 10, 2013 at 07:06 PM · c#arraygetcomponentfindscipt

Need an advise, how to find all objects with a specific name and a specific script?

So i have two scripts. Script Leaders is attached to some objests on the scene,script Follower is to move an object to objects with attached script Leaders.If i have one object everything works fine,im clicking at object for examole a cube, than im clicking at a spot,its moving to this spot. But if im having two or more objects,only one is moving or doing something.This happening i guess because its finding only one object follower = GameObject.Find("Follower").GetComponent< Follower>(); So my question is, how to make this line like to be something like an array to store information of all objects on the scene? Thanks for ant help.

public class Leaders : MonoBehaviour

{

private Follower follower;

 void Start ()
 {       
    follower = GameObject.Find("Follower").GetComponent< Follower>();            
 }
 
 void OnMouseDown() 
 {    
     if(follower.selected == true)
     {
     follower.v3Dest = transform.position;
     follower.selected = false;
     }                
 }

public class Follower : MonoBehaviour

{

 public Vector3 v3Dest;

 public bool selected = false;

 public float speed = 5.0f;
  
 void Start () 
 {
    v3Dest = transform.position;
 }


 
 void Update () 
 {        
    transform.position = Vector3.MoveTowards(transform.position,v3Dest,Time.deltaTime * speed);
    transform.LookAt(v3Dest);             
 }
 void OnMouseDown()
 {        
     selected = true;
 }
     

}

Comment
Add comment · Show 1
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 roojerry · May 10, 2013 at 07:14 PM 1
Share

look at GameObject.FindGameObjectsWithTag

you will need to use tags ins$$anonymous$$d, but it should give you all the objects you want

2 Replies

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

Answer by Bunny83 · May 10, 2013 at 11:24 PM

It's way easier to directly search for your Follower scripts. Just use FindObjectsOfType:

 public class Leaders : MonoBehaviour
 {
     private Follower[] followers;
 
     void Start ()
     {
         followers = FindObjectsOfType(typeof(Follower)) as Follower[];
     }
     void OnMouseDown()
     {
         foreach(var follower in followers)
         {
             if(follower.selected == true)
             {
                 follower.v3Dest = transform.position;
                 follower.selected = false;
             }
         }
     }
 }

ps you might want to rename your "Leaders" class to "Leader" because one instance is just a leader ;)

Comment
Add comment · Show 2 · 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 $$anonymous$$ · May 11, 2013 at 04:33 PM 0
Share

Thank you:)

avatar image $$anonymous$$ · May 11, 2013 at 04:37 PM 0
Share

But i guess for is faster,so im using:

  for(int i = 0;i<followers.Length;i++)
     {
         if(followers[i].selected == true)
         {
             followers[i].v3Dest = transform.position;
            followers[i].selected = false;
         }
     }

But Thank you :D

avatar image
0

Answer by weltraumaffe · May 10, 2013 at 11:05 PM

GameObject.Find() finds only one GameObject. With GameObject.FindGameObjectsWithTag() you can find every Game Object with a specific tag.

Beware: Find functions are very slow and shouldn't be used too often (or even repetitively in Update()...)

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

15 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

Related Questions

Get variable in script got by get component c# 2 Answers

GetComponent of Game Objects in an array 1 Answer

Distribute terrain in zones 3 Answers

Retrieve an integer from an array in a different script 1 Answer

Using getcomponent with an array 2 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