- Home /
'gameObject' is not a member of 'Object'.
I am doing a simple for loop in the start function. I added #pragma strict for optimisation reasons and now I am getting the error: BCE0019: 'gameObject' is not a member of 'Object'.
#pragma strict
import System.Collections.Generic;
import System.Linq;
var allProjectiles = new List.<GameObject>();
function Start () {
//Add every immediate child only of the transform to the list above as a gameobject
for (var child in transform){
allProjectiles.Add(child.gameObject);
}
}
If I cast the child as a gameobject in this way:
for (var child: GameObject in transform){
I get an invalid casting error instead: InvalidCastException: Cannot cast from source type to destination type.
What is the correct way to do this? I'm not getting errors in any of my other for loops, and the only similar solutions I can find on UA are for getcomponent not for loops.
Answer by Dave-Carlile · May 20, 2013 at 02:09 PM
This has been answered before...
http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html
The documentation also has an example that shows looping through children.
You're right apologies. I had seen that post before but I assumed that because it was finding transforms and I am using a list of gameObjects that it wouldn't work. Thanks a lot
Your answer
Follow this Question
Related Questions
javascript to C#, UnityEngine.Object to GameObject? 1 Answer
Cannot cast from source type to destination type 1 Answer
Collision On Specific Object= Destroy not working. 2 Answers
How can I tell what prefab a GameObject belongs to? 2 Answers
How to set GameObject's component to an instance of an object 0 Answers