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
1
Question by thomasindustry · May 07, 2015 at 09:02 PM · instantiatedynamic

What is the most efficient way to dynamically add a component, based on a string passed in.

At initialization, I want to dynamically create a large list of game objects. I will be attaching one component to each object from a set of possible scripts (all of which inherit from the same base class). An XML file will be read to tell me which script to attach for each object. What is the most efficient way to this?

Rather than having a long list of if-else-ifs

 if(type == "type1")
 {
 gameObject.AddComponent<typeOne>();
 }
 else if(type == "type2")
 {
 gameObject.AddComponent<typeTwo>();
 }
 else if(type == "type3")
 {
 gameObject.AddComponent<typeThree>();
 }

My initial thoughts are to use a look-up dictionary [string, System.type]. Is the optimal, or any other ways to approach this problem?

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 Owen-Reynolds · May 08, 2015 at 12:40 AM 0
Share

I'd go with the simplest -- the ifs you have now -- as these sorts of things tend to change a lot. For example, you may later decide to go with prefabs (with the scripts already on them.) Or may decide to list them ins$$anonymous$$d as X of each type.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by siaran · May 07, 2015 at 09:42 PM

Your initial idea sounds perfectly fine to me. A dictionary seems a sensible way to approach this, so I would go with that.

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 Bunny83 · May 07, 2015 at 10:01 PM 0
Share

+1

You can pass a string directly to AddComponent as long as the string is the name of a valid $$anonymous$$onoBehaviour class. Internally Unity probably just uses reflection to get the System.Type of that class. So a prepared dictionary would of course be faster, but not by much. How often do you add new components to your gameobjects? Even when you load in larger structures i doubt that the lookup of the class will be a bottleneck ^^.

However I just made a logic gate simulator and also create class instances by classname. I also created a Dictionary which i populate at start. In my case i don't use classes derived from $$anonymous$$onoBehaviour but derived from a certain interface. So i can't use AddComponent and have to use reflection.

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

21 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

Related Questions

Changing game object based on variable state? 1 Answer

Fastest way to instantiate thousands of objects at runtime? 1 Answer

Dynamic object Instantiate 1 Answer

Scaling Animation for a Instantiated Object 1 Answer

Checking if object intersects? 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