• 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 /
  • Help Room /
avatar image
0
Question by Anxo · Dec 01, 2015 at 10:32 PM · addcomponenttypecasting

gameObject.AddComponent(presetComponent) ?? I would like to add a component thats already created.

Lets say we have a Component called "Traits" that has some perimeters.

 public class Traits : Monobehaviour {
     public string traitName = "someone";
     public float height = 6.2;
     public float weight = 236;
     public Color hairColor = Color.black;
 }

Now what I would like to do is create a few of these and then add them to a game object later. To create them lets say I do something like this.

 public List<Traits> traitsList = new List<Traits>();
 for(int i = 0; i < 10; i ++){
      Traits newTrait = new Traits();
      newTrait.traitName = "Bob";
      newTrait.hairColor = Color.blue;
      newTrait.weight = 400;
      newTrait.height = 5.2;
 }

Now I would like to add them to a game object but I am doing it wrong somewhere.

 someNewGameobject.AddComponent<Traits>(savedTrait[i]);

Not a thing, and neither is

 someNewGameObject.getComponent<Traits>() = savedTraits[i];

Em I just approaching this wrong? I would like not to do something like.

 Traits newTrait = gameObject.AddComponent<Traits>();
 // And then have to assign all the elements on its own, that would suck if the properties are a long list.
 newTrait.traitName = savedTraits[i].traitName;
 newTrait.weight = savedTraits[i].weight;
 newTrait.height = savedTraits[i].height;
 // and so on. 

I may just have to rethink this hole spiel but it seams like there should be a way.

Thanks for checking it out.

Anxo.

Comment
Add comment · Show 3
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 meat5000 ♦ · Dec 02, 2015 at 07:41 PM 0
Share

I see that you construct a 'Traits' in your for loop but you seem to not bother adding it to your List. I assume this will cause problems.

Also, you called your List traitsList but reference savedTraits. What go on?

avatar image Bunny83 meat5000 ♦ · Dec 02, 2015 at 08:39 PM 0
Share

Also since Traits is derived from $$anonymous$$onoBehaviour you shouldn't create instances with "new". You should actually get warnings about that in your console. $$anonymous$$onoBehaviours or Components in general can only be created with AddComponent. If you create them with new they become fake null objects since they can't live without a gameobject they are attached to.

avatar image Anxo Bunny83 · Dec 03, 2015 at 08:02 PM 0
Share

Thanx you 2 but this is actually not my code. I wrote this as an example to ask the question, I should have been more thorough. $$anonymous$$y Classes are a lot longer and convoluted so I did not want to turn anyone off by posting that code here.

1 Reply

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

Answer by sankalp22 · Dec 02, 2015 at 09:14 AM

You can follow solution in this thread. here

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 Anxo · Dec 02, 2015 at 06:44 PM 0
Share

Thanks, it turns out to be way too messy, Ins$$anonymous$$d, in my case, I can get away with storing my data in a

 List<Dictionary<string,object>> Traits;

ins$$anonymous$$d. Won't work for all but allows me to avoid that reflection mess.

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

34 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

Related Questions

How to add Canvas Scaler and Graphic Raycaster to Canvas in code 1 Answer

Scripts worked but since 3 days its not working, please help. 0 Answers

Why isn't this instantiation cast working? 1 Answer

addComponent c# script by java 0 Answers

How do I change when my RigidBody 2D initializes? C# Question/Noob,How do I change when rigidbody initializes? 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