Setting a public variable doesn't work when associated with a private [SerializeField] variable set in inspector.
I've noticed that for some odd reason, if I set the value of a private variable using [SerializeField] in the inspector, it does not get set by a set property. My class is set up something like this:
public class DialogController : MonoBehaviour
{
[SerializeField] private Dialog _currentDialog;
public Dialog currentDialog
{
get { return _currentDialog; }
set { _currentDialog = currentDialog; }
}
public void OpenBoxA(Dialog d)
{
currentDialog = d;
Debug.Log("OpenBoxA result is: " + currentDialog.textChunk);
}
public void OpenBoxB(Dialog d)
{
_currentDialog = d;
Debug.Log("OpenBoxB result is: " + currentDialog.textChunk);
}
}
If I call OpenBoxA and OpenBoxB by passing them a Dialog reference, only OpenBoxB spits out the updated value. For example. If in the inspector on the DialogController component I set _currentDialog to a Dialog with a textChunk value of "this is old" and run the following code I receive inconsistent results.
private void TestOpen()
{
Dialog d = new Dialog("this is new");
dailogController.OpenBoxA(d);
// Result is "OpenBoxA result is this is old."
dailogController.OpenBoxB(d);
// Result is "OpenBoxB result is this is new."
}
The results are different, even though I think they should both give "this is new" as the output. Right? Am I missing something here?
Your answer
Follow this Question
Related Questions
How to rename a property without losing its inspector value? 0 Answers
How do I change GameObject properties in scripts Unity? 1 Answer
How do I get a serialized field saved into a PlayerPref? 0 Answers
Creating a Fixed Time to Compare Against 0 Answers
Using a Quaternion's one axis to set transform.rotation 1 Answer