[System.Serializable] ruins my code?
I have a simple class that contains a bool array and a script that sets it size. normaly it works fine but as soon I remove the code
[System.Serializable]
it returns a error on this line
if (buttons.Length != size)
I don't know why because it shouldn't effect the code in any way.
heres the intire code
[Serializable]
public class ControllerInput
{
public Vector2 leftJoystick;
public Vector2 rightJoystick;
public string buttonName = "joystick button";
public bool[] buttons;
public void GetInput(int size)
{
if (buttons.Length != size)
{
buttons = new bool[size];
return;
}
for (int i = 0; i < size; i++)
{
buttons[i] = Input.GetKey(buttonName + " " + i);
}
}
}
public ControllerInput controllerInput = new ControllerInput();
private void Update()
{
controllerInput.GetInput(19);
}
it shouldn't effect the code in any way
It can, obviously....
if you have the System.Serializable
attribute, I guess you want to display a ControllerInput
in the inspector right?
If you remove this attribute, Unity won't serialize the object, and the array buttons
will be null. If Unity serializes it, it will automatically instantiate the array for you.
You should create a constructor for the ControllerInput
and instantiate the buttons
array in it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162411im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple way to access data of different types within a nested array? 0 Answers
Remove Duplicated Objects in Array 1 Answer
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. 0 Answers
Organize data in a multi-column table 1 Answer
Creating an array of Color32 arrays 1 Answer