Accidentally editing prefab values at runtime
Hi, this may be a painfully simple question, but I've found that I am editing prefab variables at runtime rather than instances of them within the game. For example, I have an object that references a UI slider and changes it value through the use of a public variable I can assign in the inspector. I have prefabs of both of these objects. When I assign the slider's prefab to the accessing object's prefab, the instance of this accessing object within the game will edit the prefab's values of the slider rather than the slider that actually exists within the game. Is there any way to resolve this? Thank you for the help in advance!
Answer by xvivax · Sep 22, 2017 at 02:12 AM
One of the problems might be, that you try to change prefab properties before Instantiating instance into the scene. In that case it going to change prefab rather than instance (which to be fare doesn't exist).
public GameObject objPrefab;
private GameObject inst;
// Use this for initialization
void Start () {
// this will change prefab
objPrefab.transform.localScale = new Vector3(2, 2, 2);
inst = Instantiate(objPrefab, transform.position, Quaternion.identity);
// this will change instance
inst.transform.localScale = new Vector3(5, 5, 5);
}
Hey, thanks for the reply! In the case I'm working with, the slider is present in the scene from the outset and the accessor is the one that is instantiated within the game. I'm noticing now that whenever I set public variables to prefabs though, that the object will refer to the prefab itself even when there are instances of that prefab within the game. I am realizing that I can't have any external connections between prefabs, that I have to find those connections at runtime ins$$anonymous$$d.
Thanks for the help figuring that out!
Your answer
Follow this Question
Related Questions
Editing prefabs at runtime 0 Answers
How to change the enum value of an instance of a prefab being clicked 0 Answers
Unity network manager assign playerprefab at runtime 0 Answers
How to detect if any prefab is instantiated in the scene from a script? 2 Answers
How to affect only one object out of many objects with the same name using script? 0 Answers