Unity inspector can't edit public array's values

Viewed 854

I want a script that has a public array so that I can modify the values in the inspector. Here's an example script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Foo : MonoBehaviour
{
    public int[] bars;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

When I add the script to a game object, I can see the empty list in the inspector (as expected).

enter image description here

But when I add elements to the array, the array area is just blank so I can't set the value of each element (or see what the element values are).

enter image description here

Is this just a bug, or am I doing something wrong?

I'm running Unity 2021.2.5f1 on macOS Monterey 12.0.1.

2 Answers

I had this same problem after upgrading to a new version of Unity, and I discovered that it works if I move the editor window over to a different screen. Maybe because my main screen is widescreen? I dunno, I can't explain it. If I move the editor back to my main screen, they disappear again. So for now, I just adjust the values on my smaller side-screen, then move back when I'm done. If you don't have more than one screen, perhaps try to change the resolution or something.

May be not the best solution but, definin your own serializable int struct may works.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public struct MyInt
{
    public int Int;
}

public class Foo : MonoBehaviour
{
     public MyInt[] bars;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
Related