- Home /
setting variables on a clone's component instead of the prefab?
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
Answer by Tasarran · Jul 06, 2011 at 05:56 PM
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);
}
Your answer
Follow this Question
Related Questions
How to instantiate from prefab, not from instance? 3 Answers
Sometimes one sometimes two clone is formed. I just want to form a clone 1 Answer
Pass a copy of a GameObject as variable to another script? 1 Answer
How to have different textures for cloned gameobject (instantiated) 1 Answer
How to delete instantiated GameObject 4 Answers