• 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 Cousken · Jan 30, 2013 at 07:33 PM · prefabs

Creating a list of prefabs indexed by enums

Hello!

First question here:)

I'm trying to create a list of prefabs with an enum as the index so that i can easily and quickly instantiate these prefabs from code. The list will be quite long if i manage, i want to put every game object there. The intended use would be somehting like this:

 public List<GameObject> myPrefabs = new List<GameObject>();
 
 public GameObject CreateGraphic(Graphic aGraphic)
 {
    GameObject newGraphic = GameObject)GameObject.Instantiate(myPrefabs[(int)aGraphic]);
         return newGraphic;
 }

EDIT: The question is, how do i fill the List with Prefabs in the enum order? I realize i that if i make the list public i can drag and drop prefabs in the editor, but that seems unreliable to me as there is no actual connection to the enum, just very careful and mistake prone ordering.

What i hope to find is a way to find these prefabs from code so i can enter them in the correct positions at Start(), using the enum as index.

Is this possible in any way? I tried with public dictionaries and hash tables, no luck. Am i going about this the wrong way?

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 fafase · Jan 31, 2013 at 08:17 AM 0
Share

But I would guess you have a limited amount of prefabs, you are notcreating them at runtime. You can go with array then.

avatar image Cousken · Jan 31, 2013 at 08:25 AM 0
Share

Yes you are right, array works well. However it't not the question of what datatype to use, just how to get the prefabs into it in the correct order :)

2 Replies

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

Answer by Berenger · Jan 31, 2013 at 08:28 AM

If your enum is going from 0 to x, you can use (int)theEnum. But if it starts at 5, or if the order can change, you can use a dictionary to link the enums with an index.

 Dictionary<Graphic, int> dico = new Dictionary<Graphic, int>();
 
 dico.Add( Graphic.something, 0 );
 dico.Add( Graphic.somethingElse, 1 );
 ...
Comment
Add comment · Show 3 · 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 Cousken · Jan 31, 2013 at 08:55 AM 0
Share

This is very true, but dont dictionaries have worse performance than arrays?

Still, the questions is how to get the prefab in script, while it is not present in the scene, only in assets. I manage to put them in a data structure just fine :)

avatar image Berenger · Jan 31, 2013 at 09:07 AM 0
Share

O$$anonymous$$, I see your point. Here is another thought.

You want to load an asset based on an enum, but there is no connection between them. If you don't affect them somewhere in the inspector, you'll have to use Resources.Load. To know which one to load for a given enum :

  • Create an array of strings with the names of the prefabs to load, that match the order of the enum. No real diff with affecting the prefab directly.

  • Create that same connection enum / asset in an external file, like xml. At runtime, load it, deserialize, and load the assets based on that.

  • $$anonymous$$yEnum.FirstEnum.ToString() will result in "FirstEnum". So you could name each asset after the enum (or the other way around, then call (Resources.Load( aGraphic.ToString )).

PS : those are three different ways, not three steps.

avatar image Cousken · Jan 31, 2013 at 09:09 AM 0
Share

Thanks that really does answer my question :) Will try it out Asap.

avatar image
0

Answer by PAEvenson · Jan 30, 2013 at 09:21 PM

I think you are looking for

 using System.Collections.Generic; //need to include this to use List
 
 
 List&lt;GameObject&gt; myprefabs = new List&lt;GameObject&gt;();



 

You can then drag them into the inspector.

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 Cousken · Jan 31, 2013 at 08:16 AM 0
Share

Thanks for the reply! It made me realize that i wasn't clear about what the problem was. It was late, i apologize :) As you can see i was already using a list, the problem is more putting the prefabs in it in the correct order from script. I made an edit to make the question clear.

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

13 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

Related Questions

Invisable Projectile; Instantiate at Collisions 2 Answers

Place stars (prefabs) in random locations 3 Answers

Instantiating A Grass Prefab Multiple Times 1 Answer

How to shoot prefab, and then on colliding destroy a gameobject? 2 Answers

How to spawn a prefab in a cone in front of player? 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