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
2
Question by xEnOnn · Jan 09, 2013 at 09:01 AM · c#gameobjectunityeditorprefabsbestpractices

Must I always create a public variable for Prefabs?

Currently in a script called TestBehaviour.cs, I have a public variable called PicFrame in the MonoBehaviour class. This class is attached to an empty GameObject in the scene. In my assets folder, I have designed a picture frame GameObject, which is composed of several other children GameObject with textures on them. I made that into a Prefab.

Within the Unity Editor, I dragged the picture frame Prefab to the PicFrame variable to assign to it. And within the TestBehaviour.cs, every time before I want to use it, I will do an instantiate call on the Prefab assigned to PicFrame variable.

This is how I have been working with assets in my game.

However, I am not sure if this is an efficient way of doing things. In a situation, I have several possible picture frame Prefabs that could be assigned to the PicFrame variable. It depends on the game's current state that a particular picture frame prefab is used. I could declare 10 public variables, drag 10 prefabs to assign to each of the 10 public variables, and then decide within the code to use which of the 10 variables. When I want to add more frame Prefabs, I will add more public variables, and drag a new Prefab to it. Of course, I could make all of it into a List, but still I will still have to drag the Prefabs. Moreover, if I needed other types of Prefabs, I would have still have to declare new public variables for assignment.

Is what I have been doing the correct and ideal/best practice when working in Unity? If not, what is the best practice?

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

2 Replies

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

Answer by robertbu · Jan 09, 2013 at 10:19 PM

Rather than use 10 variables, consider use an array. You can set the size and then drag each Prefab to a different index.

 public PicFrame[] arPicFrame;

If you wanted to automate, you could use scene game objecst rather than prefabs, tag the scene object, and the initialize the array with GameObject.FindGameObjectsWithTag().

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
1

Answer by dorpeleg · Jan 09, 2013 at 02:37 PM

I think the way you are doing it right now is good, but here is another possible solution:

Use Resources.Load

and with a bit of smart programming I think it's possible to make it load all prefebs in the Resources folder (even if you keep adding more) without changing the code.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Problem to play animation in C# 5 Answers

Game Object is Instantiating on same position 2 Answers

** why does my Enum parameter goes back to default value? ** 1 Answer

How would I find the prefab of a gameObject? 2 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