GameObject.GetComponentsInChildren returns twice as much objects than i thought it would
Hello) Im trying to fill my public GameObject[] elemArr
with values) Im doing this in this code
private void Awake()
{
Field = GameObject.FindWithTag("Name:SpawnTerrain");
//Init elemArr
{
Collider[] bColArr = GameObject.FindWithTag("Name:AdditionalElemContainer").GetComponentsInChildren<Collider>();//Arr that contains all colliders, help me to fill GameObject arr
Array.Resize(ref elemArr, bColArr.Length);//resise emelArr(Before his size was 0 i think)
for (int elemArrN = 0; elemArrN < elemArr.Length; elemArrN++)
{
elemArr[elemArrN] = bColArr[elemArrN].gameObject;
Debug.Log(elemArr[elemArrN]);
}
}
//
}
More precisely in //Init elemArr
part. In hierarchy i have empty object that has has "Name:AdditionalElemContainer" tag and is parent for 6 objects(5 of them has colliders like box, capsule or sphere collider) Then i thought that after end of awake() function my console'll have 5 messages but it has 10(first 5 are similar to next 5)
Tell me please why is this happen and how can i fix it? It would be helpful i think :)
Answer by Bunny83 · Jul 15, 2020 at 08:16 PM
Are you sure you do not run that code twice? There are several possibilities how and why that might happen.
First you might have attached this script several times, maybe even to different gameobjects. Each instance will of course perform the same actions.
Another reason might be that for some reason you manually call your Awake method again. Hopefully this is not the case ^^. This could be easily be ruled out be checking the stack trace for each debug log to see where the message comes from.
To detect which actual object has created your log messages you can add a context object to each log statement like this:
Debug.Log("Element: " + elemArr[elemArrN].Name, gameObject);
Note that second parameter gameObject
. This will "tie" this message to this object. When you click on the debug log message in the console, Unity will "ping" that object in the hierarchy. That should make it easy to find out which instance created this message. Also note that I've changed your log message. You should avoid just logging single values or objects without any context. I've seen countless people printing float values all over the place so in the end you get tons of numbers in the console and don't even know which number belongs to which log call.
Answer by NiliSmiles · Jul 16, 2020 at 04:51 AM
Thank you for the answer) There was two objects with this script attached to them) And both had array with size 5)
Your answer
![](https://koobas.hobune.stream/wayback/20220613003228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is Casting in Unity ? 1 Answer
Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer
SetActive is just working if i press twice in the first time 2 Answers
How are those Resident evil/Silent hill type level transitions done? 1 Answer
How can I put buttons on a Cube? 0 Answers