How to create a List containing all the different variables within a script (regardless of type)?

Hey, so what I’m trying to do is create a generic list that contains all of the variables on a given object that can be accessed from within the inspector. So, if my script has stored references to a GameObject and an AudioSource, I want these to be added to the list.
I don’t want a list of just GameObjects, or just AudioSources, I want both. Can I do this? Or is making a bunch of type-specific lists really my only option?

Most Unity classes inherit from the “Object” class at some point, so you could use that for GameObjects and Components. This only works for classes, there’s no base clase for primitive types like float or int.

If you want that on runtime you’ll have to use reflection to get the fields of the class for each component.

If you’re making editor code, use the SerializedObject and SerializedProperty classes, which handle all the serialized data of components. If you can set a reference in a field on the inspector and it gets serialized, then there’s a SerializedProperty for that field of type ObjectReference with that Object reference.

try this

using UnityEngine;

class MyObjectBlueprint
public GameObject myGameObject;
public string myGameObjectName;
public AudioSource audioSource;
what ever you need

Then in another script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

class MyClassName : MonoBehavior
public List<MyObjectBlueprint> myListName;

void Start
myListName = new List<MyObjectBlueprint>();