Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Quinnje012 · Oct 28, 2017 at 02:28 AM · editorscripting-underthehood

are there drawbacks to exposing variables/game objects through the editor?

I want to know what are (if any) the drawbacks to making a variable of type GameObject public in a script?


Background Information:

I have a map with a bunch of nodes it. I have the nodes organized so that from east to west we have our starter nodes, each starter node has multiple children that runs north to south until a certain point. I am loading all the nodes into a multidimensional array, however I have recently found out that Unitys "Findxxx" does not follow the hierarchy. Since I now have to manually pick each starter node and their children in sequential order, I am worried about having loops within loops...within yet more loops. My idea to cut back on the nested loops is to public expose an array for the starter nodes, and load them sequentially by hand in the editor (total of 56 starter nodes). Generally as a rule of thumb, I try to avoid making anything public (I blame my old professors for this) so before I do this I just want to find out if there are some reason(s) to avoid exposing variables/game objects through the editor?

Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 29, 2017 at 10:16 AM 0
Share

While I disagree with the accepted answer, I think if you're having to hook up an array of 56 nodes in the inspector, then there's going to be as better way to do it.

Hard to say exactly what without having more details, but it'll probably be based on looking at the immediate children of each node recursively (using transform.transforms rather than Find), so that the algorithm is tracking a path through the hierarchy.

3 Replies

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

Answer by Desoro · Oct 29, 2017 at 10:16 AM

1: Any scene-based reference variables (non-prefabs) should be serialized and controlled via a manager which can be referenced via a singleton (Manager.instance.serializedVar).

2: Anything you want to make a prefab, you cannot serialize anything other than other prefabs or internal references (components attached to children).

3: Contrary to MezFo's advice, avoid filling reference variables inside a script. This is hard-coding conditions which can (and usually will) change over the development of the project. Serialized reference variables will never break as long as the referenced object still exists. Even components on the same object as the script are better off serialized.

4: Never, EVER, use GameObject.Find or any other "Find" methods in Unity. It's horribly inefficient and there is no reason a coding structure should ever do this.

Unity is built around components and serialization, use the engine the way the developers intended and you will avoid a lot of headaches.

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 Quinnje012 · Oct 30, 2017 at 11:44 PM 0
Share

Okay, I think this has given me some new insight and I will be looking into serialization. Thank you for the heads up and pointing me down a different path. The originally accepted answer I think has been removed so i am marking yours as correct. Thanks!

avatar image
2

Answer by Adam-Mechtley · Oct 29, 2017 at 09:35 AM

Please also remember that you do not need to make fields public to expose them in the editor. You can decorate any private field with the SerializeField attribute and it can be edited in the Inspector without exposing it to your API surface.

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
avatar image
0

Answer by michi_b · Nov 11, 2017 at 02:53 AM

Exposing a GameObject in the inspector is certainly not a bad thing in general. Follow the same rule as you should with any kind of behaviour member variable: only expose it if you actually intend to set it via the inspector.

But in your case since you wrote that you do it just because

"Findxxx" does not follow the hierarchy

I would say implement your own find method that traverses the hierarchy the way you expected it? Mostly because assigning too much stuff manually can become tedious as your project grows and makes your inspectors more complex than they need to be. And, as other users noted, exposing stuff in inspector does not mean it has to be public and having assigned the references ahead-of-runtime will give you better performance (which is probably not an issue if you do it only at startup)
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

100 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

Related Questions

Unity version dependent deprecation, possible? 1 Answer

Is it possible to drag/drop/increment into editor array? 1 Answer

How to prevent the scene view from panning when I right-click my mouse? 0 Answers

Unity Editor Inspector creating gameObjects in world 2 Answers

Unity inspector scripting - scriptable objects - card game 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