- Home /
Script on prefab keeps values from one execution to another
A prefab that contains a script and is used inside another GameObject script will keeps values from one execution to another.
Example:
I have a script A that defines a private member d which is of type int[] initialized with {1, 2, 3}.
Inside the script, I have a method called Alter that adds 1 to each values of d.
I have a script B that has a private member of type A with the attribute SerializedField.
In the start method, it calls the alter method of the member of type A.
Then I have a prefab P that have a component of type script using A.
Finally, I have a GameObject that have a component of type script using B.
The first time it executes, the values for d will be {2,3,4}.
The next execution, the values for d will be {3,4,5} and so on for other executions.
I was wondering if it was the expected behaviour and why it does that ?
Are you supplying it through the inspector? If your intention is to use that supplied value as a starting point, then you will need to create a copy of it during initialization.
Answer by DiegoSLTS · Apr 24, 2019 at 06:55 PM
When you're in Play Mode on the editor you have to be careful not to change any asset, those changes will not be discarded when you leave Play Mode. I guess that your problem is caused cos you're modifying the GameObject field that where you dropped the Prefab, and that's equivalent to modifying the asset itself.
You should Instantiate a prefab before making any change or calling any function and make sure you do that on the Instance itself. Usually your code should look something like this:
public class SomeClass : MonoBehaviour {
[SerializeField]
GameObject prefabReference;
void SomeFunction() {
GameObject newInstance = Instantiate(prefabReference);
AnotherScript comp = newInstance.GetComponent<AnotherScript>();
comp.Value = 10;
comp.GreatFunction();
}
}
Make sure you have something like that instead of:
public class SomeClass : MonoBehaviour {
[SerializeField]
GameObject prefabReference;
void SomeFunction() {
AnotherScript comp = prefabReference.GetComponent<AnotherScript>();
comp.Value = 10;
comp.GreatFunction();
}
}
Or:
public class SomeClass : MonoBehaviour {
[SerializeField]
GameObject prefabReference;
void SomeFunction() {
Instantiate(prefabReference);
AnotherScript comp = prefabReference.GetComponent<AnotherScript>();
comp.Value = 10;
comp.GreatFunction();
}
}
I saw the last one a few times, some people Instantiate a prefab but don't store the new object's reference and keep working over the prefab instance.
Note that all this only happens while playing the game in the editor, the game can't modify bundled asset when running from a build. Something similar could happen when modifying materials.
Your answer
Follow this Question
Related Questions
My prefab isn't getting destroyed 1 Answer
Animator is not playing a Playable 2 Answers
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
How to Instantiate prefab when dragging an object through a specific area? 1 Answer
How to obtain this prefab name in this circunstances 1 Answer