- Home /
How would I change the value of one prefab's variable via script without changing all of them?
I am making a game using javascript where several characters are moving around a level on their own. The player starts out with one character. After they tell that character to begin, the game creates the next character for the player to send into the level by creating a prefab of the first one (which means that it has the same script attached to it as the original character). This works fine in some cases and I can get several characters into the level, but when one of them hits a trigger which changes one of it's variables, it changes the variable on all the other prefabs as well. How can I make it so that each prefab acts completely independently of the others.
If that wasn't very clear I'll try giving this example: The player clicks on the first character. The first character begins walking and the second character is spawned. The player clicks on the second character. The second character begins walking and the third character is spawned. By this point, the first character has reached a trigger which is supposed to act as a teleporter and move them to a specific position. However, all the created characters are moved to that position instead of just the first character.
Thanks for any help.
This would happen if you are using static variables, and there might be other causes. Please post your code.
Okay. So here's an example of something that works the way it's supposed to and one that doesn't. ($$anonymous$$y characters are squirrels by the way.)
var squirrel : GameObject;
function OnTriggerEnter (theCollision : Collider)
{
if(theCollision.gameObject.tag == "treeTrigger")
{
isClimbing = true;
}
else if(theCollision.gameObject.tag == "homeEntrance")
{
//$$anonymous$$oves squirrel to top of tree
yield WaitForSeconds(1.0);
squirrel.transform.position = homeExit.transform.position;
}
}
If I have multiple squirrels in play and one touches the treeTrigger, it changes the isClimbing variable to true for just the squirrel that touched the treeTrigger. But if I have multiple squirrels in play and one touches the homeEntrance trigger, all of the squirrels in play will be teleported to the location of the homeExit. I'm guessing this has to do with me referencing the squirrel GameObject variable before transform.position but I don't know how to change it's location without telling it what I want to teleport.
There's nothing wrong with this code that I can see. Something else is going on. Do you have a sample project you can post?
Answer by TrickyHandz · Oct 01, 2013 at 03:22 AM
There is a chance that the "squirrel" variable is always being set to the first "squirrel" game object. I would have to see the rest of you code to see if that is the case. However, rather than referencing a variable, just call transform directly so that the object is always calling its own transform and there is no chance for confusion:
function OnTriggerEnter (theCollision : Collider)
{
if(theCollision.gameObject.tag == "treeTrigger")
{
isClimbing = true;
}
else if(theCollision.gameObject.tag == "homeEntrance")
{
//Moves squirrel to top of tree
yield WaitForSeconds(1.0);
// Change this gameObject's transform.position to
// the same as homeExit.transform.position
transform.position = homeExit.transform.position;
}
}
Yep. That did it. I thought you had to declare the GameObject you wanted to transform. Now that I know not adding the GameObject will only change the status of the object the script is connected to it should make things much easier. Thank you.
Your answer
Follow this Question
Related Questions
Load Prefabs in Array with Javascript 1 Answer
Does enabling/disabling Scripts have a high performance cost? 1 Answer
Bullet Instantiation 1 Answer
What variables can i declare? 1 Answer
Assigning variables to GameObjects 2 Answers