How can I assign a Component 'instance' to a Game Object?

Hello friendly friends!

As I’m learning Unity, I stumbled upon this interesting and frustrating bit.

Say I keep a list of something I call Unit.

And I have a prefab empty game object with my Unit script as a Component.

I wanna loop through my list of Units, instantiate a GameObject from my prefab, and then assign its Unit component to be the unit I’m currently looping on.

I couldn’t actually find a way to do this! I tried AddComponent but apparently that doesn’t let you add an instance, I tried GetComponent = myUnit and this was also apparently terrible…

I ended up doing something that just feels wrong:

foreach (Unit u in friendlies) {
			GameObject unit = Instantiate(combat_unit_prefab) as GameObject;
			unit.GetComponent<Unit>().tileX = u.tileX;
			unit.GetComponent<Unit>().tileY = u.tileY;
			unit.GetComponent<Unit>().character = u.character;
			unit.GetComponent<Unit>().map =;


Isn’t it silly?

Surely there must be a better way than having to assign each unit’s variables.

If you share in your knowledge and wisdom, I will be grateful!

What is “friendlies” an array of? It sounds like you might want:

foreach (Unit u in friendlies) {
    GameObject unit = Instantiate(u) as GameObject;