- Home /
How to keep certain values on MonoBehaviour Reset method.
The API documentation implies that the Reset method can selectively keep the values of assigned fields.
Code example from MonoBehaviour.Reset in Unity Script Reference:
public class example : MonoBehaviour {
public GameObject target;
void Reset() {
if (!target)
target = GameObject.FindWithTag("Player");
}
}
However, when I try to do this it seems that references are nullified before Reset is executed. For instance:
protected void Reset () {
if (m_nextNode == null) {
Debug.Log("m_nextNode = null");
}
}
The message is logged regardless of the value of m_nextNode in the inspector. Am I doing something wrong or is there an error in the reference?
Yes, the values get nullified before Reset is called. What gave you the impression that they weren't? The Editor does other stuff before running the Reset method on your monobehaviour. There's no error in the documentation: nowhere does it indicate that it should work any differenty.
This is what I thought, but the example code from the reference suggests otherwise; Why would you check for a null value if there is no other possibility?
@fecal_brunch: good point, it is contradictory to check if a value is null given that all values always get nullified. It would only make sense in a scenario where you are overriding the Reset method from a super class, and there would exist the possibility of such super class already initializing a given value. But nevertheless, the Unity example inherits directly from $$anonymous$$onoBehaviour, so...
Answer by rhys_vdw · Mar 16, 2013 at 04:50 AM
It is not possible. The fields are reset before the Reset function is called.
Your answer
Follow this Question
Related Questions
Calling MonoBehaviour's Reset() method 0 Answers
Prevent 'Paste Component Values' on MonoBehaviour 1 Answer
Problem with Reset and Custom Inspector 2 Answers
My inspector icons keep resetting, can anyone help? 0 Answers
namespaces and script names 0 Answers