- Home /
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?
All Unity related objects inherit from UnityEngine.Object.
Everything inherits from object (c#).
So Object is, probably, what you are looking for, beats me if it is getting serialized though.
But why? How are you planning to handle all said Object uniformly?
Hey, thanks. I had a project get totally fucked up after updating and lost all the object connections I had made in the editor, so I wanted to figure out a way to prevent that from ever happening again. So basically what I'm trying to do is create a script, or rather a function that I can include in other scripts, that will essentially store all of the object references I make in the editor in empty prefabs, where the name of those prefabs are the names of the objects that are being referenced in the other script, so that in another function I can restore any lost connections by using GameObject.Find using the name of the prefab I created to automatically replace them if it sees that the field in question is empty. Hope that makes sense, haha
$$anonymous$$inda flawed. From what I make out of this, is you are trying to create an equivalent system to the existing one, which is using GUIDs.
I am not saying it won't work, but I doupt it will be as robust as the existing one. Some of the problems I see, is the separetion of prefab references and sceene objects, also rena$$anonymous$$g stuff will end up braking it.
$$anonymous$$y suggestion would be, first of all, avoid actions that may cause such event. Updating the editor midst of development, is by all means discouraged even from Unity (major updates).
$$anonymous$$eep backups, regardless of updating or not.
Organise your project file system to make it easy to restore the damage alongside proper na$$anonymous$$g.
And lastly avoid editor references as much as possible.
That been said I would use attributes to mark the fields ought to be saved, each time a change occure on the editor hook to the event(not sure if such event exist with proper arguments) to save and or change the associated data and also you ll need an event(if there isn't one) when changes occure to field names and prefab names (last two events will be different).
All that should be done with editor scripts and if possible without using objects as GameObjects/ ScriptableObjects maybe use Json or xml. And only apply said data backwards via some manual input.
Best of luck!
Answer by DiegoSLTS · Apr 20, 2019 at 03:01 AM
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.
I think this is the answer.
To expand, just create a
List<Object> list = new List<Object>();
and most variables should go in.
Note: Object may be supposed to be object
Answer by DaresFireson · Apr 20, 2019 at 10:16 AM
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>();
}
}
Your answer
Follow this Question
Related Questions
How to Copy a Component if it contains List? 1 Answer
Adding more than one item to the list and saving it into JSON file problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to modify array values? 1 Answer