- Home /
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.
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()
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
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.
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.
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..."
It's really powerful - but really funky when you aren't used to it. Unity is a very opinionated framework.
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.
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
Follow this Question
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