How to define a SerializedObject as Array on Custom Inspector Script?

Hello everyone, maybe it’s something simple that i can’t find or i don’t know,
I can get int, float or object properties but I cant get the array value of a SerializedProperty in a custom inspector script

I made a script that used by selection of rotated body parts of a 2D character, actually it is completed, I made all built-in values private and I made read-only versions of the same values that used in custom inspector script but I’m just asking because I was wondering is there a method like that? My second question is does public access modifiers cause security vulnerability?

the script:

I will try to explain with a clear and simple example as possible:

on base.cs:
[SerializeField] GameObject[]  someGameObjects;
[SerializeField ]int anyInt = 0;
 
on custom inspector editor.cs:
SerializedObject m_GameObjArrayElement;
SerializedObject m_IntVal;
OnEnable()
{
       m_GameObjArrayElement = serializedObject.FindProperty("someGameObjects");
       m_IntVal = serializedObject.FindProperty("anyInt");
}
OnInspectorGUI()
{
          m_IntVal.IntValue = 5;  // I was able to get the int value with the conversion function inside the SerializedProperty class itself, How can I convert array values like that?
     
          main( m_GameObjArrayElement.ArrayValue() );  // i need a command that works like in this field "m_GameObjArrayElement.ArrayValue()", but I couldn't find anything like this..
 
}
 
void main(GameObject[] gameObjects)
{
       //some operations
}

Greetings, I’m not sure what you are trying to achieve entirely but I will do my best to help. From the looks of it, you want to make an array of game objects and use the game objects from that array but can’t get those game objects to be assigned to anything. There are a few issues with your code if that’s what you are trying to achieve.


First off your initial array declaration is not public or serialized so if you are trying to assign objects through the game inspector it won’t work since it is a private variable. Try instead:

public GameObject[] someGameObjects;

or:

[SerializeField] GameObject[] someGameObjects;

The first option will allow you to call those game objects from other scripts, the second will still serialize in the editor but you won’t be able to call them from other scripts. If you declare an array like either of the options above, you are not defining exactly how many variables you want in the array which makes it very flexible because you can add objects as needed. When you declare it like you did later in your script:

GameObject[] test = new GameObject[3];

you are saying I want an array that can hold exactly 3 variables. You can assign your game objects directly in the editor or like so:

void Start()
{
      someGameObjects[0] = GameObject.Find("Game Object Name");
}

Notice how I did it in the Start method that runs before the first frame to declare the variable.


Secondly, you have your variables all over the place and they aren’t talking to each other like you think they are. There are two types of variables, global and local. Any variables you want to be able to be called in your whole script must be declared up top before any methods:

public GameObject[] someGameObjects;

void Start()
{
     someGameObjects[0] = GameObject.Find("Game Object Name");
}

If instead you do it like this:

void Start()
{
       GameObject[] someGameObjects;
       someGameObjects[0] = GameObject.Find("Game Object Name");
}

void Update()
{
       Debug.Log(someGameObjects[0]);
}

it will produce an error since as far as the Update method is concerned there is no variable known as someGameObjects since it was not declared globally.


Finally, you need to keep in mind that arrays follow zero based indexing which means someGameObjects[0] is the first object in the array, someGameObjects[1] is the second and so on. I would highly highly recommend doing a intro c# course at the very least to learn some of these basics before trying to delve into this or it will be a very long journey. If you can, purchase The C# Players Guide book from amazon and go through that, it will help you a lot. Some further reading for you now: Arrays - C# Programming Guide | Microsoft Learn Declaring and Initializing Variables in C# | Pluralsight