Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by JestaBROOK · Feb 20, 2019 at 02:52 PM · gameobjectinstantiatecasting

Instantiate with and without casting as GameObject

Hey, I'm having a tough time getting my head around what is going on when I Instantiate.

If I do the following:

 public GameObject prefab;
 public GameObject prefabClone;
 
 void Start()
 {
      prefabClone = Instantiate(prefab, transform.position, transform.rotation);
      Debug.Log(prefabClone);
 }

I get the output: prefab(Clone) (UnityEngine.GameObject)

Visual studio also gives me all of the functionality of a GameObject without explicitly casting it like this:

 prefabClone = Instantiate(prefab, transform.position, transform.rotation) as GameObject;

Alternatively, if I load the object dynamically from the Resource folder like so:

 public GameObject prefab;
 public GameObject prefabClone;
         
 void Start()
 {
      prefabClone = Instantiate(Resources.Load("awsomePrefab"));
      Debug.Log(prefabClone);
 }

Then the methods and properties are hidden from me (until I explicitly cast "as GameObject"). However, the output is the same regardless (of type GameObject).

In the first example when I am referencing a prefab of type GameObject, and storing it in the variable of type GameObject is the cast made implicitly now? What is the need to cast "as GameObject" if all of the GameObject methods and properties are exposed to me. Thanks for your help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JedBeryll · Feb 20, 2019 at 03:57 PM

Resources.Load("awsomePrefab") will load a UnityEngine.Object because it doesn't know the type of the item you want to load. You could use the same method to load a texture or anything but it doesn't know what you want unless you specify it like Resources.Load<GameObject>("awsomePrefab").

Same goes for Instantiate which could also take the type parameter like Instantiate(prefab), but you don't have to specify it because "prefab" is a GameObject so it is done implicitly. Now if you use Instantiate and Resources.Load together, you are loading an Object and Instantiating an Object.

 Resources.Load("awsomePrefab"); //returns Object
 Resources.Load<GameObject>("awsomePrefab"); //returns GameObject
 Resources.Load("awsomePrefab") as GameObject; //returns GameObject
 
 Instantiate(Resources.Load("awsomePrefab")); //returns Object
 Instantiate(Resources.Load<GameObject>("awsomePrefab")); //returns GameObject
 Instantiate(Resources.Load("awsomePrefab") as GameObject); //returns GameObject
 Instantiate(Resources.Load("awsomePrefab")) as GameObject; //returns GameObject

Comment
Add comment · Show 5 · 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
avatar image JestaBROOK · Feb 20, 2019 at 04:02 PM 0
Share

So in the following example:

     public GameObject prefab;
     public GaneObject prefabClone;
     
     void Start()
     {
         prefabClone = Instantiate(prefab, transform.position, transform.rotation);
     }

Then I do not need to Instantiate "as GameObject" because this is implicitly done? That's what I expected. Thanks.

avatar image JedBeryll JestaBROOK · Feb 20, 2019 at 04:06 PM 0
Share

That's right!

avatar image JestaBROOK JedBeryll · Feb 20, 2019 at 06:34 PM 0
Share

Hey sorry to bother you again. But I still can't find any documentation that says that the cast is implicit. Take this example from the documentation:

 // Require the rocket to be a rigidbody.
 // This way we the user can not assign a prefab without rigidbody
 public Rigidbody rocket;
 public float speed = 10f;
 
 void FireRocket () 
 {
     Rigidbody rocketClone = (Rigidbody) Instantiate(rocket, transform.position, transform.rotation);
     rocketClone.velocity = transform.forward * speed;
     
     // You can also access other components / scripts of the clone
     rocketClone.GetComponent<$$anonymous$$yRocketScript>().DoSomething();
 }

What is the point of the explicit cast here? Since it should be implicitly set as a Rigidbody. All of the documentation and other threads say that Instantiating returns an Object, and you must cast the Object returned to GameObject/Rigidbody/Transform. The official tutorial (2:18) is also guilty of this.

Show more comments

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.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is Casting in Unity ? 1 Answer

Object to GameObject casting error c# (from object in scene not a prefab) 0 Answers

How can i Instantiate gameobject in array 0 Answers

NullException Error while instantiating gameobjects 0 Answers

Instantiate duplicate of inactive child in GameObject 0 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