- Home /
The public variables of a prefab dont reset when I open the game.
Hello I have a problem. I have a prefab with a public variable. In other script, I create a instance of the script that have the prefab with "get component", and modify this. But, when I close the game, and play one more time, the prefab dont reset to his normal values, instead, he still with the changes the other script makes in the game process, why is this?
Thank you
Do you really create an instance? Sounds like you are in fact creating a reference.
What do you modify? If you modify things in the project folder that are not instantiated (Like the sharedmaterial of a renderer for instance) they are saved in the file in your assetfolder and thus persist. Or maybe you change the prefab's value ins$$anonymous$$d of the instantiated value? Anyway, a code snippet containing the Instantiate part and the 'modifying' would be helpful.
Ok, I need to access to a script called "$$anonymous$$ovement", and this script is in a prefab of an asteroid. inside the script I have a public variable, "velocity" for control the velocity of the asteroids.
ok, then I have an empty object, with a script with the game logic. I need to acces $$anonymous$$ovement script, for change the variable "velocity
public GameObject asteroid;
$$anonymous$$ovement movement;
void Start()
{
movement = asteroid.GetComponent<$$anonymous$$ovement>();
}
...
Void level2()
{
movement.velocity+=5;
}
in theory, in the game the variables change, but then, when the procces stops, the value should be the original, but isnt... And change the prefab. What I do wrong?
Thanks for the help.
Answer by brentatplayful · Nov 10, 2014 at 06:13 PM
It would be helpful to see your code but I'm sure it's that you're using the actual prefab instead of a instantiated GameObject. Once you have a reference to the prefab, Use GameObject.Instantiate( prefab );