Hello,
I’m trying to instantiate a prefab, then change some of its variables on a component called PlayerStats. When I run the game, any changes I make to the variables affect the prefab’s script component and not the clone. Here is my script,what am I doing wrong?
var pc : GameObject;
var pcScript : PlayerStats;
var minStartVal : int = 40;
function Start()
{
Instantiate (pc, Vector3.zero, Quaternion.identity);
pc.name = "New Player";
pcScript = pc.GetComponent(PlayerStats);
pcScript.endurance.SetBaseValue(minStartVal);
pcScript.coordination.SetBaseValue(minStartVal);
}
thanks!
Matt
The problem here is that you define ‘pc’ as the prefab.
(I assume you are dragging the prefab into the ‘pc’ slot in the Inspector.
What you need to do is define a variable when you instantiate, like this:
var pc : GameObject;
var pcScript : PlayerStats;
var minStartVal : int = 40;
// new var for the instantiated version
var myPC : GameObject;
function Start()
{
// You can set a var as you instantiate, then that var deals with the instantiated object.
myPC = Instantiate (pc, Vector3.zero, Quaternion.identity);
// changing 'pc' to myPC causes these to deal with what you just instantiated
MyPC.name = "New Player";
pcScript = myPC.GetComponent(PlayerStats);
pcScript.endurance.SetBaseValue(minStartVal);
pcScript.coordination.SetBaseValue(minStartVal);
}