- Home /
[SOLVED] Global Variable in C# class seems to be acting like a local variable.
public class ProblemClass : MonoBehaviour
{
//The custom class does not inherit from MonoBehaviour
[SerializeField, HideInInspector]
public CustomClass[] customClasses;
[HideInInspector]
public int someInt;
private void OnValidate()
{
Initialize();
}
void Initialize()
{
someInt = 10;
customClasses = new CustomClass[6];
for (int i = 0; i < 6; i++)
{
customClasses[i] = new CustomClass();
}
Debug.Log(someInt);
Debug.Log(customClasses);
}
public void SomeFunctionCalledFromAnExternalScript()
{
Debug.Log(someInt);
Debug.Log(customClasses);
}
}
The above is an abstract and simplified version of my code. The weird thing is that when accessing the variables in the function below after they'd been initialized only the int variable has a value while the Array of the Class is null. I'm new to c# and just can't explain this behavior. Does anyone know how to get the variable to keeping its value? Removing the Serialization doesn't make a difference.
Answer by claudius_g · Jul 19, 2019 at 09:10 AM
I was able to solve the problem: I forgot to add the [System.Serializable]
attribute to the CustomClass, which blocked saving...
Your answer
Follow this Question
Related Questions
Custom Class Array Serialization Problem 0 Answers
Unsure on how to access a var from another function 2 Answers
Keeping object between edit and play modes. 1 Answer
Saving array data? 2 Answers