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
0
Question by PsychoDuckArcade · Aug 23, 2014 at 09:07 PM · gameobjectprefabvariablereferenceprivate

How do I assign a prefab reference to a private variable?

 private GameObject myPrefab;

I need to assign a prefab reference to this variable, but because Unity uses public and private to indicate what to show and make editable in the inspector, this ends up being quite a problem:

I can't reference prefabs from the Assets folder in code.

I can't manually drag the prefab reference onto the script in the inspector as the variable is private.

I suppose I COULD just make it public but that'd be horrible programming practice making a variable that shouldn't be accessible from the outside public instead of private, just so I can actually initialize the thing.. Not to mention even if I did add it as a new public variable, all my derived classes don't get updated with the new value, so I'd have to go through each instance of a derived class individually and click reset (if there's a quicker way to handle that, I'd really like to know).

I also don't want to use Resources.Load as then I'd have to pass in strings of the prefab names instead of actual references, which is a huge problem if I ever decide to rename some prefabs which I most likely will.

I can kind of see why Unity uses public and private by default to determine what to show in the inspector, but how do I override this behaviour? How do I manually tell it what variables to expose or hide regardless of whether or not they happen to be public or private? As it is, this automatic variable hiding feature is limiting what I'd otherwise be able to do, at least with private variables.

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

Answer by Arkuni · Aug 23, 2014 at 09:24 PM

The answer you seek, is the SerializeField attribute:

 [SerializeField]
 private GameObject myPrefab;

This attribute will tell Unity to serialize the field, which means that the value will be saved when going into Play-Mode. It will also make the field popup in the editor.

Comment
Add comment · Show 1 · 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 PsychoDuckArcade · Aug 23, 2014 at 10:13 PM 0
Share

Awesome, now the private field shows up in the inspector, but it still doesn't keep the value consistent among all derived classes. How do I update the value for all derived classes? Hitting reset on each individual instance of a derived component is just not an option.. Is there like, a reset all button somewhere?

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

23 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

Related Questions

Is it possible to set up a prefab variable such that it can only be modified on the prefab and not on the instance? 3 Answers

Setting a game object from a list to be the active character that gets instantiated 1 Answer

Disable GameObject through Prefab? 2 Answers

How to make reference from prefab to variable via C#/JavaScript? 1 Answer

How to use a float value from coroutine 1 and use in coroutine 2? 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