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.


                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?


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?

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.

  1. Try putting the type in quotes.
  2. Try using the fully qualified type name, UnityEngine.Renderer
  3. You might need to have "typeof(Renderer)" replace "Renderer"
  4. 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<cs.Length();i++) {

    if (cs _!= null && (cs *is Renderer))*_

    // now use rs as your array of renderers.

  5. 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<subcomponents.Length();i++) {

    *_</em> <em><em>_var c:Component = subcomponents*;*_</em></em> <em><em>_*if (! (c is Renderer))*_</em></em> <em><em>_*continue;*_</em></em> <em><em>_*var r:Renderer = (Renderer)c; // or try: c as Renderer*_</em></em> <em><em>_*subRenderers.Push(r);*_</em></em> <em><em>_*}*_</em></em> <em><em>_*

The script reference does say what classes inherit from, including the Transform and GameObject classes.