- Home /
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?
I think that function Start will not wait and that's all :)) Do another function with yield and then put that function in start.
function Start(){ plswork(); }
function plswork(){ Populate(1, 2, 1, 0, 0, -28); yield WaitForSeconds(1); Populate(2, 14, 2, 0, 0, -22); }
Does it work? :/
Thank you! That was a major help! Now I can finally move on with my project. Can you throw that in as an answer so that I can check it off for you?
Answer by William_Lee_Sims · Jun 16, 2015 at 05:39 AM
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.
Answer by Bonfire-Boy · Jun 15, 2015 at 10:27 PM
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.
Your answer
Follow this Question
Related Questions
Instantiated coconuts not working 0 Answers
Using Instantiate on a Rigidbody rather than a whole GameObject 1 Answer
Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers
Instatiate prefab at another prefab issue 1 Answer
How could I create a prefab from an exsisting gameobject at runtime? 2 Answers