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 EVOin3D · Apr 11, 2014 at 04:59 AM · c#arraygetcomponent

GetComponent of Game Objects in an array

Hi, I'm having trouble with the following script:

 public class Circle : MonoBehaviour 
 {
     public GameObject[] neighbours;
 
     private Circle circle;
 
     void Awake ()
     {
         foreach (GameObject neighbour in neighbours)
         {
             circle = neighbour.GetComponent<Circle>();
         }
     }
     
     void OnMouseDown ()
     {
         circle.Toggle();
     }
 
     void Toggle ()
     {
         // do stuff
     }
 }

In my scene is a bunch of circles, each with the Circle script attached. I need each circle to have access to its neighbours, so that when I click on one it toggles it toggles them.

Problem is, with the script above it's only toggling the last neighbour in the array. How do I GetComponent of each game object in the array?

Comment
Add comment · Show 7
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 flamy · Apr 11, 2014 at 05:01 AM 0
Share

can you post a pic of your heirarchy! It is hard to understand what you are trying here!

avatar image robertbu · Apr 11, 2014 at 05:11 AM 0
Share

It is unclear what you want here. Ins$$anonymous$$d of making neighbors an array of GameObjects, make it an array of Circle objects. Dragging and dropping to initialize the array will get the Circle component of each object. In On$$anonymous$$ouseDown() you can access any/all of the circle components.

avatar image EVOin3D · Apr 11, 2014 at 05:12 AM 0
Share

As in the hierarchy in Unity? It's just a list of game objects.

Imagine there are four circles like so:

• ○ • •

When you click the white circle, it toggles its neighbouring circles.

avatar image EVOin3D · Apr 11, 2014 at 05:18 AM 0
Share

How would I go about doing that Robert? I tried:

 public Circle[] circle;

But it gave me the error "Cannot implicitly convert type 'Circle' to `Circle[]'"

avatar image EVOin3D · Apr 11, 2014 at 05:55 AM 0
Share

Thanks for the suggestion guys. I ended up just using Send$$anonymous$$essage ins$$anonymous$$d, which works fine.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Apr 11, 2014 at 06:14 AM

SendMessage is pretty slow and when possible should be avoided.

In your case, you want each object to toggle its neighbour. Since they all have their neighbour in the Circle script you can use a recursive process:

 public Circle neighbour;
 public bool toggleOn = false;
 
 public void ToggleNeighbour()
 {
    if(neighbour =! null && neighbour.toggleOn == false){
        neighbour.ToggleNeighbour();
    }
 }

Same applies if you use an array of Circle, it will only take a little longer since there is more to do.

Other solution is to define a tree of neighbour and use a queue or a stack, see there for an example http://unitygems.com/tree-search/

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 EVOin3D · Apr 11, 2014 at 06:41 AM 0
Share

Thanks, that seems like a good approach but I'm not sure how to implement it. Where exactly does it fit into my existing code?

Also, for what I need to do the performance hit of Send$$anonymous$$essage isn't a problem.

avatar image fafase · Apr 11, 2014 at 01:18 PM 1
Share

$$anonymous$$y answer was more of an alternative but if efficiency is no issue then go for the easy way -> Send$$anonymous$$essage.

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

Find GameObjects with a true boolean and put them in an array? 1 Answer

Using getcomponent with an array 2 Answers

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

Multiple Cars not working 1 Answer

Retrieve an integer from an array in a different script 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