Search Unity

Casting Object to GameObject

Discussion in 'Scripting' started by StarBright, Nov 17, 2009.

  1. StarBright

    StarBright

    Joined:
    Oct 14, 2009
    Posts:
    34
    I have an object created using the Instantiate function and I want to cast it to a GameObject, but Unity won't let me. Why not, since the object it's creating should be a GameObject? And what do I do about it?
     
    Yoriko likes this.
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the code you are using?
     
  3. StarBright

    StarBright

    Joined:
    Oct 14, 2009
    Posts:
    34
    One of the instance variables is

    public Transform avatar;

    which is set to the correct prefab in the inspector. The Start method is OK with the line

    Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

    or

    Object o = Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

    but won't allow me to cast the object to a GameObject.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Have you tried something like:-
    Code (csharp):
    1. GameObject go = (GameObject) Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);
    ...and found it doesn't work?
     
    ri3m and Yoriko like this.
  5. StarBright

    StarBright

    Joined:
    Oct 14, 2009
    Posts:
    34
    I've tried that. It instantiates the object, but I get an InvalidCastException.
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    How about:-
    Code (csharp):
    1. GameObject go = (GameObject) Instantiate (avatar.gameObject, new Vector3(0, 0, 0), Quaternion.identity);
     
  7. StarBright

    StarBright

    Joined:
    Oct 14, 2009
    Posts:
    34
    No, then it says "'UnityEngine.Transform' does not contain a definition for 'GameObject'".
     
  8. spree

    spree

    Joined:
    Apr 20, 2009
    Posts:
    20
    Don't know if you figured it out yourself, but I ran into the same problem today and thought I'd tell what the problem seems to be.
    The object returned by Instantiate() is not a GameObject, but a Transform. So you can cast it to a transform and then get to the GameObject if you have to.

    Code (csharp):
    1. Transform t = Instantiate(prefab, pos, rot) as Transform;
    2. GameObject go = t.gameObject;
    Quite silly, because the documentation says otherwise. I guess they changed this some time ago and forgot to update the docs... :(
     
  9. 苗双和

    苗双和

    Joined:
    Jun 7, 2011
    Posts:
    1
    The right solution. Thanks alot :)
    miao
     
  10. efryt

    efryt

    Joined:
    Dec 3, 2011
    Posts:
    5
    you could also try:

    Code (csharp):
    1. GameObject go = GameObject.Instantiate( ... ) as GameObject;
     
    Vaupell, Gizensha and edselink like this.
  11. Zarack

    Zarack

    Joined:
    Dec 19, 2012
    Posts:
    6
    Spree, I could kiss you.
     
    unity_murc134 likes this.
  12. arckex

    arckex

    Joined:
    Nov 17, 2013
    Posts:
    3
    I would also like to kiss you.

    Funny note: my friend and I are both working on this project via git. His doesn't throw this error, and mine does. So, there's something else going on here.
     
  13. segment

    segment

    Joined:
    Oct 31, 2013
    Posts:
    2
    I'll tell you more. I can successfuly do `Instantiate(prefab) as GameObject;` but in another class I get an error. What can I say...Unity!
     
  14. Stakker

    Stakker

    Joined:
    Oct 15, 2014
    Posts:
    1
    Ahhhh... thank you for the solution :)
     
  15. Deleted User

    Deleted User

    Guest

    Not true. Instantiate returns a UnityEngine.Object which is different from System.Object.
    This should work as well:

    Code (csharp):
    1. GameObject t = Instantiate(prefab, pos, rot) as GameObject;
     
    GlassTempleGames likes this.
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Wow. Ton of weird misinformation in this thread.

    Instantiate returns a UnityEngine.Object. The actual runtime type of this object is the same as the type of the object you pass in. So if you pass in a Transform you can cast the result to a Transform. If you pass in a GameObject you can cast the result to a GameObject. Same with a Component.

    In any case, this is all pretty moot now, as there is a generic version of Instantiate available as of 5.0 (maybe earlier). The generic version eliminates the need to cast altogether.
     
  17. Deleted User

    Deleted User

    Guest

    The generic version does not work for the Instantiate version they're talking about. It only works for Instantiate that takes 1 parameter. The Instantiates that takes 3 parameters can only take and return a UnityEngine.Object.
     
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It appears from the docs you are right. That's an odd design choice. Either way my comments about the type that is returned is still valid. There is no mystery about it, you get the same runtime type out as what you put in.

    So this works

    Code (CSharp):
    1. Transform myTransform;
    2. ...
    3. Transform clone = (Transform) Instantiate (myTransform);
    As dose this

    Code (CSharp):
    1. GameObject myObject;
    2. ...
    3. GameObject clone = (GameObject) Instantiate (myObject);
    And the same principle for any thing else, rigidbody is a common one I use.

    But this will not work, and will throw a runtime error.

    Code (CSharp):
    1. Transform myTransform;
    2. ...
    3. GameObject clone = (GameObject) Instantiate (myTransform);
     
  19. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The generic version of Instantiate is declared with T restricted to UnityEngine.Object. This means that you can call Instantiate on things that are not Components or GameObjects, which means that they don't have an attached transform to feed the second and argument to.

    The type argument can't have any better restrictions, since the method needs to accept both GameObjects and Components, and both of those inherit directly from UnityEngine.Object.

    It should probably accept those arugments anyways, and throw an error if you send in something like a ScriptableObject or whatever, but maybe there's use cases where things that doesn't have an attached transform is valid.
     
  20. unity_murc134

    unity_murc134

    Joined:
    Mar 9, 2019
    Posts:
    1
    Thanks mate, this worked for me as well!