• 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
2
Question by KostasBs · Oct 17, 2017 at 07:15 PM · prefabprefabs

Best way to reference a prefab

Let me start by saying that the differences between GameObject and Transform are pretty clear to me so please don't give me an answer of that type.

I noticed that, from within a script, when I want to access a prefab I can achieve it in both ways:

public GameObject prefab;    // This works
...
Instantiate(prefab);
...

public Transform prefab;    // This works too
...
Instantiate(prefab.gameObject);
...

I noticed that no matter which I use I can pass a prefab from the unity editor and do anything I want with it later on, using the appropriate functions.

Now my question is, is any of these two incorrect in any way? Can any of them lead to a weird behaviour? Is there a "best coding practice" that defines how to reference a prefab? Finally does it have any difference performance-wise (In the way unity treats the reference or something)?

Comment
Add comment · Show 2
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 StormMuller · Oct 17, 2017 at 08:31 PM 0
Share

It's good to see people, thinking about what is the correct way of doing things and not just how to do them in the quickest way.

avatar image StormMuller · Oct 17, 2017 at 08:34 PM 0
Share
 public Transform prefab;    
 
 Instantiate(prefab); // This should also work, because "prefab" is still a unity object

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html https://docs.unity3d.com/ScriptReference/Object.html

1 Reply

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

Answer by Hellium · Oct 17, 2017 at 07:18 PM

There is no "best way".

The advantage to reference a Component (such as Transform), is that you can force your designer to do provide a gameobject holding the given component. For example : public Rigidbody prefab if you want to have the instance with a Rigidbody.

If you only declare public GameObject prefab, your designer could potentially put any gameobject he wants.

Comment
Add comment · Show 4 · 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 KostasBs · Oct 17, 2017 at 07:32 PM 0
Share

When you pass the prefab in the script at the game editor, doesnt unity apply any conversions? That was my first thought because in order for the object to adapt to both some things should happen in the background. From your answer I understand that this gives $$anonymous$$imal performance overhead, is that correct? I know its a small detail but sometimes these things bug you :) and also my project relies heavily on prefab instantiation so any small difference may improve/decrease performance.

avatar image Hellium KostasBs · Oct 17, 2017 at 07:48 PM 2
Share

I don't know what Unity does in the background, but I don't think it does any "conversion". When a gameobject is dragged to the public field, Unity just checks if the component is attached to it.

Yes, you will have increase a little bit the performance, but I don't think it will be noticeable. It's mostly used to make things easier for your designers and prevent some mistakes.

avatar image StormMuller · Oct 17, 2017 at 08:28 PM 1
Share

Yea all components hold a reference to the gameobject they're attached to. So calling transform.gameObject has no performance hit. But there are some good practices you might want to follow. If you want to really decouple your design. Then use GameObject ins$$anonymous$$d of Transform as it is more abstract. Unity by default will force you to have a transform for each gameobject in the scene. But lets say you publicly exposed a rigidBody ins$$anonymous$$d of a gameobject. and later on you create a player that uses a playerController ins$$anonymous$$d of a rigidbody you would have to restructure your code to accommodate the change.

If you wanted to force the use of a component you could use rigidbody ins$$anonymous$$d of gameobject.

What I prefer doing is using inheritance and abstract classes to force the use of a specific type. This allows me to unit test my code. (Unity won't show interface types in the inspector, which sucks. But there are work arounds.)

avatar image KostasBs StormMuller · Oct 17, 2017 at 09:05 PM 0
Share

Usefull information about Component enforcement and unit testing, thanks a lot :)

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

90 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

Related Questions

Prefabs shown as blue boxes and not previews 0 Answers

Link GUIText to a prefab? 2 Answers

Zombie is working fine but it's prefab is creating issue in animation in unity,Zombie is working fine but it's prefab is creating issue in animation in unity Ask Question 0 Answers

Apply Changes to Prefab - Unity 2018 0 Answers

Updating a prefab fbx or animation while using version-control is broken 4 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