• 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 Javok · Mar 08, 2012 at 12:14 AM · gameobjectobjectprefabsinstantiation

Is it necessary to assign the result of Instantiate() to a variable and is there a need to always typecast?

If you're already using a variable pre-typed to GameObject which stores the prefab via the inspector, is it proper to just call Instantiate statically using that public inspector var as an argument? It stores the prefab and does instantiate, but I'm a bit confused by how Unity C# expects to work with the instance at this point because I saw another example where a post-type as casting was used...

eg, C#

 public GameObject somePrefab;    // drag prefab into exposed inspector field
 
 void Start()
 
 {
 
    Instantiate(somePrefab, startPosition, startRotation);
  
 }

versus say:

 somePrefab = Instantiate(somePrefab, startPosition, startRotation) as GameObject;

or

 somePrefab = Instantiate(GameObject)Instantiate(somePreb, startPosition, startRotation);

I guess it's a bit confusing that the public var above is already typed to GameObject, stores the prefab via the inspector and yet Instantiate returns type of Object not GameObject? I've combed through the q/a but would love some simplified summary of this issue. Thanks to any who may shed some light.


Well looks like I figured this out, I think. It takes one public variable to store the prefab from the editor and another variable to store the instance resulting from the Instantiate() call. I get it. What threw me off was that Instantiate() was returning an Object type, not a GameObject and so none of my GameObject dependent code was able to use my instance until I recast it as a GameObject. I had to the solution but was too dull to realize it!

Comment
Kleptomaniac

People who like this

1 Show 0
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

  • Sort: 
avatar image

Answer by rutter · Mar 09, 2012 at 11:15 PM

Instantiate() returns a reference to the created object in case you need to do anything further with it. If you don't, there's no need to store the value.

If whatever you instantiated is a GameObject, it should always be safe to cast the return as one.

The reasoning for this is fairly complicated and tricky to explain in a nutshell, but if you're curious: it has to do with something called polymorphism. Instantiate() takes and returns an Object reference because it can be called to create objects from many different classes, all of which inherit from UnityEngine.Object. This makes the function very powerful, but is a little bit confusing if you don't know about it.

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 2 Answers

object vs gameobject 2 Answers

problem of switching character in the game 0 Answers

inspector,Component 0 Answers

How to tell at runtime if a GameObject is a prefab 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