Quantcast
Channel: Latest Questions by Ubahs
Viewing all articles
Browse latest Browse all 4

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

$
0
0
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(); 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[] gameLayers = this.gameObject.GetComponents(); 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.

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>