- Home /
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?
can you post a pic of your heirarchy! It is hard to understand what you are trying here!
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.
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.
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[]'"
Thanks for the suggestion guys. I ended up just using Send$$anonymous$$essage ins$$anonymous$$d, which works fine.
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/
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.
$$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
![](https://koobas.hobune.stream/wayback/20220613142442im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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