- Home /
Instantiate GameObject, GetComponentsInChildren Cast Type Error - JS
I am sorry to ask this question again, but after hours of research and more trial an error, there seems to be no way i can figure this out.
I am taking one part of a large GameObjects hierarchy, instantiating it and trying to get all the renderer components of the new instance and its children.
Code:
var tempObject : GameObject = Instantiate(objectToHighlight, objectToHighlight.transform.position, objectToHighlight.transform.rotation) as GameObject;
Debug.Log(tempObject); //at this point it says in the console that it is in fact a GameObject..
var childrenRenderers : Renderer[] = tempObject.GetComponentsInChildren(Renderer);
I have read all that I can find about the oddness of Instantiating and what it returns, some say its a Component, others say it is a Transform. Either way, i did a direct type cast to GameObject and it appears to work, according to the console. But says my typecast is incorrect on line 3, where i GetComponenetsInChildren for the Renderers.
I have also tried Instantiating into a Transform, then typecasting that into a GameObject, but i receive a null object reference.
What do you master scripters think?
---EDIT---
One thing I think might be causing me grief is that the object I am instantiating has children, that might not be getting converted to gameobjects. It seems like my tempObject variable is only holding the info for the parent of the instance, and when doing the conversion to GameObject, it only does this one object and not the children, which might part of the problem?
Thanks for co$$anonymous$$g our timberland online store! Just enjoy yourself here! As a developping company, timberland boots always can give us some surprise .I belive timberland shoes Sale can make your feet more comfortable, make your life more stylish!If you want to have a try ,just click here: discount timberland boots Free delivery
Answer by msknapp · Sep 24, 2011 at 09:12 PM
A component and a GameObject are mutually exclusive things. GameObject can have Components, but GameObjects are not themselves components. A transform is a subclass of Component, so is any collider, renderer, monobehaviour, MeshFilter, and many other things. You cannot try casting a GameObject as a Transform, and you cannot try casting a transform as a GameObject. You will get an exception. This is why you should really be using C# instead of javascript, you get strong type casting checks so you would solve this problem faster.
Still... I don't see what's wrong with your script. But I have a few ideas for you to try.
Try:
var g:GameObject = GameObject.Instantiate(prefab,position,rotation) as GameObject;
var cs: Component[] cs;
cs = g.GetComponentsInChildren(UnityEngine.Component);
var rs :Array;
for (var i:int = 0;i if (cs[i] != null && (cs[i] is Renderer))
rs.Push(cs[i]);
}
// now use rs as your array of renderers.
If all else fails, you could try this
var subcomponents:Component[] = myGameObject.GetComponents();// or getComponentsInChildren(); var subRenderers:Array = new Array(); for (var i:int = 0;i
var c:Component = subcomponents[i]; if (! (c is Renderer)) continue; var r:Renderer = (Renderer)c; // or try: c as Renderer subRenderers.Push(r);
}
The script reference does say what classes inherit from, including the Transform and GameObject classes.
Your answer
Follow this Question
Related Questions
Reference an Instance of an Object?C# 1 Answer
GetComponent vs AddComponent 3 Answers
Get components of multiple instantiated objects? 2 Answers
What's the best way to grab script references after instantiation 1 Answer
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers