Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
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
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
0

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
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.

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

Destroy all Objects of a Type in Game 1 Answer

Problem creating a script to destroy object on collision. 1 Answer

Transform.localPosition Script Problem. 3 Answers

Assets/SkyAir.cs(13,25): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.Object' 3 Answers

Destroy object last on application close. 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