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
0
Question by Ubahs · Apr 01, 2013 at 09:45 AM · addcomponent

Adding same Type to gameObject.AddComponent repeatedly keeps modifying first component.

Hi there, just started using Unity today and I've hit a wall that I can't figure out.

I'm adding multiples of a custom MonoBehaviour (GameLayer) class to another custom MonoBehaviour (GameScene) but, when I add them and modify the values I'm just modifying the first Component over and over:

 GameLayer gl = this.gameObject.AddComponent<GameLayer>();
 gl.name = layerName;
 gl.Width = width;
 gl.Height = height;
 this.Layers[i] = gl;

After doing this several times, this.Layers is full of references to the same object. I've even tried:

 gl = this.gameObject.AddComponent<GameLayer>();
 GameLayer[] gameLayers = this.gameObject.GetComponents<GameLayer>();
 gameLayers[i].name = layerName;
 gameLayers[i].Width = width;
 gameLayers[i].Height = height;
 this.Layers[i] = gameLayers[i];

In-case AddComponent was only returning the first one.

Like I said, this is my first day with Unity, and this is all in editor. I've written an editor script to generate my level meshes (this is part of that). So, perhaps I'm doing something wrong because of it being in editor (like daring to touch an object's material)?

Any help is appreciated.

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 whydoidoit · Apr 01, 2013 at 04:26 PM

So I'm not sure why you are adding the same component multiple times (it seems like GameLayer should be a normal class owned by a component), but it appears as if the effect you are seeing is that AddComponent returns the result which you would get with GetComponent - i.e. the first thing attached. It's probably a bug as this is an odd use case - don't think I ever added the same component more than once to a single object.

Anyhoo if you use Linq you could get the last component from a GetComponents call using .Last()

Comment
Add comment · Show 5 · 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 whydoidoit · Apr 01, 2013 at 04:28 PM 2
Share

It's probably just those first few hours of "What the hell is this component thing" that we all suffer when we start with Unity :p

avatar image Ubahs · Apr 01, 2013 at 04:53 PM 0
Share

Sadly, I can't use .Last() because it's adding the same reference over and over. Everything in that collection is the same object (the instantiated most recent GetComponent().

I feel like I have a good enough grasp of components (I could be wrong!). I could most likely do this another way, but I'd like these layers to have their own transform and components themselves.

avatar image whydoidoit · Apr 01, 2013 at 04:55 PM 1
Share

Right so let's get to what you are trying to do:

A component doesn't have a transform - it shares the one on the GameObject it is attached to. So presumably you'd want to have it on a separate object in that case.

avatar image Ubahs · Apr 01, 2013 at 05:31 PM 0
Share

You, sir, are a scholar and a gentleman.

Changed GameLayer to not being derived from anything. Added a member GameObject and new that up during instantiation of GameLayer.

I wish the documentation went a little more into what Components are actually for, when you should use them, etc. Then I won't say something stupid like "I feel like I have a good enough grasp of components..."

The Component-Script Relationship

avatar image whydoidoit · Apr 01, 2013 at 05:37 PM 0
Share

It's really powerful - but really funky when you aren't used to it. Unity is a very opinionated framework.

avatar image
0

Answer by Nidre · Apr 01, 2013 at 10:03 AM

I am not sure but did u tried adding the same component by using the editor multiple times ?

Cause maybe Unity is not allowing more than one instance of this component in the same game object.If you can't add multiple instances of the component by using editor try placing them into the differen gameobjects.Maybe that could work.

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 Ubahs · Apr 01, 2013 at 01:39 PM 0
Share

No luck, I just tried that and the editor does allow multiple GameLayers to be added via the editor.

That is, in debug, the this.Layers[] array got filled up as expected. Thanks.

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

12 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

Related Questions

Trying to child multiple GameObjects to one master object in script 1 Answer

How Do I Get a Script to Reference the GameObject When Using AddComponent? 1 Answer

Creating prefab through custom editor window 1 Answer

What is happening when you use greater and lass than signs with AddComponent? 1 Answer

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent() 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