I’m stumped!
I have one instance of a prefab placed in the scene (by hand in the editor), with a script (built into the prefab) that makes it stretch taller by simply adding a fixed amount to its scale each Update.
Then after a moment, I instantiate a second instance of the same prefab. (It has NO parent.)
Here’s the problem: The new instance spawns with the stretched-out scale that the first one has attained, rather than the prefab’s own native starting size which is much smaller.
Since parenting is ruled out (I explicity set it to null to be sure), what else could possibly affect an instance’s scale at the moment it appears? How could one instance be connected to the scale of the earlier instance? (Note: once created, I can scale it independently of the other. It’s just the creation scale that’s wrong, not a permanent connection.)
The prefab is the ONLY thing in the scene (except for a light and cam) and is constructed as follows:
-
A model with one script attached to make it grow.
-
A second model as a child of that, with a script that contains only a single function: one that causes a new instance to spawn when the first script calls on it. (Why 2 scripts instead of one? For future reasons I will need.)
-
What gets instantiated, of course, is another copy of the same prefab, with the same two models and same two scripts. So the process repeats, creating a third instance, a fourth, etc. (One every 2 seconds.) All share the same problem: they start at the final size of the last one instead of starting over small again. (I put them all at the origin to keep things simple.)
Here’s the child model’s script (“WeedBud2”) that spawns a new prefab instance when called (and this works fine):
var stem : GameObject;
function Branch () {
var instantiated : GameObject = Instantiate (stem, Vector3.zero, Quaternion.identity);
instantiated.transform.parent = null;
}
And here’s the parent model’s script that makes any given instance grow taller over time (which also works fine) and then calls on the function above:
private var childbud : WeedBud2;
childbud = gameObject.GetComponentInChildren(WeedBud2);
function Start () {
StartCoroutine("GrowthDone");
}
function Update () {
transform.localScale.y = transform.localScale.y + .5*Time.deltaTime; //Grow taller
}
function GrowthDone () {
yield WaitForSeconds (2); //Grow for 2 seconds only, then Branch spawns new prefab
childbud.Branch ();
Destroy (this);
}
That’s it… no other scripting in the scene. What should I be looking for to explain why the second one spawns at the size of the first?
Note: I can be sure the prefab is not simply set to a large native size: I can vary size the first instance reaches (by changing the growth time), and the second one always matches. (And then grows from there–so each instance ends up bigger than the last.)
Thanks for any thoughts.
EDIT: I’ve stripped the project down to bare bones (new code pasted above) and it’s still happening :?