Prefab Variables Changing in the Original, Not the Copies

I’ve made a cube prefab and when it’s created, it receives values from the main camera’s script.

var player : Transform;
var thescript = player.GetComponent("Stats");
var hitBoxCount : int;
function Start () {
	Populate(1, 2, 1, 0, 0, -28);
	yield WaitForSeconds(1);
	Populate(2, 14, 2, 0, 0, -22);
}

function Populate(Player : int, Character : int, Team : int, x, y, z){	
	Instantiate(player, Vector3(x,y,z), transform.rotation);
	player.GetComponent("Stats").Player = Player;
	player.GetComponent("Stats").Character = Character;
	player.GetComponent("Stats").Team = Team;
}

The values make it through, but only to the original prefab that I have in the Project panel, not the ones in the Hierarchy. I know this because whenever I start and restart the script, the original’s values change from 0, to the first set of parameters of Populate, to the second and it just gets stuck there. I reset the values of the “Stats” script, but it does the same thing again. Does anyone have any idea how to just edit the “Clones” of a prefab and not the original?

Do two things:

  1. Remove the yield statement from your Start() function.

  2. Assign your “Instantiate(…)” to another variable like “var pClone = Instantiate…”. Then use that variable to do the rest: “pClone.getComponent(“Stats”).Player = Player;” and so on.

So, player is your prefab, right? What you’re doing is making a clone and then setting values on the object that you cloned, rather than on the clone. You need a reference to the clone, which is what’s returned by Instantiate.

So replace line 11 with

Transform clone = Instantiate(player, Vector3(x,y,z), transform.rotation);

and then change the following lines to set values on clone rather than on player.