• 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
0
Question by frmusta · Sep 24, 2011 at 12:52 AM · gameobjectinstantiategetcomponentchildren

Instantiate GameObject, GetComponentsInChildren Cast Type Error - JS

I am sorry to ask t$$anonymous$$s question again, but after hours of research and more trial an error, there seems to be no way i can figure t$$anonymous$$s out.

I am taking one part of a large GameObjects $$anonymous$$erarchy, instantiating it and trying to get all the renderer components of the new instance and its c$$anonymous$$ldren.

Code:

                 var tempObject : GameObject = Instantiate(objectToHighlight, objectToHighlight.transform.position, objectToHighlight.transform.rotation) as GameObject;
                 Debug.Log(tempObject);             //at t$$anonymous$$s point it says in the console that it is in fact a GameObject..
                 var c$$anonymous$$ldrenRenderers : Renderer[] = tempObject.GetComponentsInC$$anonymous$$ldren(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 GetComponenetsInC$$anonymous$$ldren 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 t$$anonymous$$nk?

---EDIT---

One t$$anonymous$$ng I t$$anonymous$$nk might be causing me grief is that the object I am instantiating has c$$anonymous$$ldren, 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 t$$anonymous$$s one object and not the c$$anonymous$$ldren, w$$anonymous$$ch might part of the problem?

Comment
Add comment · 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by msknapp · Sep 24, 2011 at 09:12 PM

A component and a GameObject are mutually exclusive t$$anonymous$$ngs. 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 t$$anonymous$$ngs. You cannot try casting a GameObject as a Transform, and you cannot try casting a transform as a GameObject. You will get an exception. T$$anonymous$$s is why you should really be using C# instead of javascript, you get strong type casting checks so you would solve t$$anonymous$$s 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.GetComponentsInC$$anonymous$$ldren(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 t$$anonymous$$s

    var subcomponents:Component[] = myGameObject.GetComponents();// or getComponentsInC$$anonymous$$ldren(); 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
    Add comment · 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

    How to Access Components in Children of GameObject? 1 Answer

    Instantiate a copy without its children 0 Answers

    Only change a variable on the instaniated object not the prefab. 0 Answers

    Accessing instantiated GOs nested in prefabs with GetComponent 4 Answers

    Limiting respawns on scene 1 Answer


    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