• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by frmusta · Sep 24, 2011 at 12:52 AM · gameobjectinstantiategetcomponentchildren

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?

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image timberland8989 · Sep 25, 2011 at 03:37 AM 0
Share

Thanks for coming 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

timberland

timberland boot

timberland shops

timberland 2011

discount timberland

wholesale timberland

timberland chaussures

1 Reply

· Add your reply
  • Sort: 
avatar image

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 putting the type in quotes.
  • Try using the fully qualified type name, UnityEngine.Renderer
  • You might need to have "typeof(Renderer)" replace "Renderer"
  • 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.

    Comment

    People who like this

    0 Show 0 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users

    Your answer

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    Welcome to Unity Answers

    If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

    Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

    Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

    Follow this Question

    Answers Answers and Comments

    4 People are following this question.

    avatar image avatar image avatar image avatar image

    Related Questions

    Reference an Instance of an Object?C# 1 Answer

    Get components of multiple instantiated objects? 2 Answers

    GetComponent vs AddComponent 3 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


    Enterprise
    Social Q&A

    Social
    Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

    Footer

    • Purchase
      • Products
      • Subscription
      • Asset Store
      • Unity Gear
      • Resellers
    • Education
      • Students
      • Educators
      • Certification
      • Learn
      • Center of Excellence
    • Download
      • Unity
      • Beta Program
    • Unity Labs
      • Labs
      • Publications
    • Resources
      • Learn platform
      • Community
      • Documentation
      • Unity QA
      • FAQ
      • Services Status
      • Connect
    • About Unity
      • About Us
      • Blog
      • Events
      • Careers
      • Contact
      • Press
      • Partners
      • Affiliates
      • Security
    Copyright © 2020 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell My Personal Information
    • Cookies Settings
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Explore
    • Topics
    • Questions
    • Users
    • Badges