- Home /
How can i access all the objects with whom my script is attached to ???
I am only able to access only first child( there are 5 more) , i want to access all 6 at once . I am talking about this line ChangeColor ChangeColor1 = thePlayer.GetComponentInChildren();
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class onClick1 : MonoBehaviour {
public Text ScoreText;
private int Count = 10;
private int wrongAnswer = -5;
public bool hello;
public void SayHello( bool hello = false ) {
if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
DisplayColor displayColor = thePlayer.GetComponentInChildren<DisplayColor>();
ChangeColor ChangeColor1 = thePlayer.GetComponentInChildren<ChangeColor>();
hello = true;
displayColor.changeColor();
ChangeColor1.ColorME();
ScoreText.text = "SCORE: " + Count;
} else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
hello = false;
ScoreText.text = "SCORE: " + wrongAnswer;
}
}
}
Answer by Cherno · Jun 13, 2015 at 08:18 PM
You are using GetComponentInChildren. This will only return one of the specified component. What you need to use instead is GetComponentsInChildren. Note the "s" indicating plural ;)
Edit: The complete code:
DisplayColor[] displayColors = thePlayer.GetComponentsInChildren <DisplayColor>();
foreach(DisplayColor displayColor in displayColors) {
Debug.Log(displayColor.gameObject.name);
displayColor.changeColor();
}
ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
foreach(ChangeColor changeColor in changeColors ) {
Debug.Log(changeColor .gameObject.name);
changeColor.ColorME();
}
Its not working ,i am getting this error - CS0029: Cannot implicitly convert type ChangeColor[]' to
ChangeColor'
Take a look at the scripting API I linked to again. GetComponentsInChildren returns an array of components, which makes sense since you want to be able to get more than one.
DisplayColor[] displayColor = thePlayer.GetComponentsInChildren<DisplayColor>();
ChangeColor[] ChangeColor1 = thePlayer.GetComponentsInChildren();
You would then be able to itereate through the array to access one of the stored components.
ok i have tried but i still getting this error- CS1061: Type ChangeColor[]' does not contain a definition for
Color$$anonymous$$E' and no extension method Color$$anonymous$$E' of type
ChangeColor[]' could be found (are you missing a using directive or an assembly reference?)
Now it cant locate my "Color$$anonymous$$E " function
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class onClick1 : $$anonymous$$onoBehaviour {
public Text ScoreText;
private int Count = 10;
private int wrongAnswer = -5;
public bool hello;
public void SayHello( bool hello = false ) {
if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
DisplayColor displayColor = thePlayer.GetComponentInChildren <DisplayColor>();
ChangeColor[] ChangeColor1 = thePlayer.GetComponentsInChildren<ChangeColor>();
hello = true;
displayColor.changeColor();
ChangeColor1.Color$$anonymous$$E();
ScoreText.text = "SCORE: " + Count;
} else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
hello = false;
ScoreText.text = "SCORE: " + wrongAnswer;
}
}
Sorry, I used GetComponentInChildren ins$$anonymous$$d of GetComponentsInChildren :rolleyes:
DisplayColor[] displayColors = thePlayer.GetComponentsInChildren <DisplayColor>();
foreach(DisplayColor displayColor in displayColors) {
Debug.Log(displayColor.gameObject.name);
displayColor.changeColor();
}
ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
foreach(ChangeColor changeColor in changeColors ) {
Debug.Log(changeColor .gameObject.name);
changeColor.Color$$anonymous$$E();
}
How do you know I'm not a girl? Well, I'm not so don't get your hopes - or anything else - up :P
I had no internet for the last week but I'm glad that you found my help... helpful! I pasted the final, correct solution into the original answer. Feel free to mark it as accepted to help otehr users sharing the same problem.
Edit: About the lag: Note the the "Find(...)" functions are pretty resource-intensive. It might be better to just store the desired GameObject in it's own variable at game start.
Your answer
Follow this Question
Related Questions
Accessing other GameObjects Script Variables 2 Answers
How to create gameobject with different setting 1 Answer
Hi im trying to set a Gameobject variable in a script not in the inspector. 3 Answers
Access main camera from different GameObject script 1 Answer
Prefab Script List not being assigned as unique - can you see why? 1 Answer