- Home /
Updating Public Class Array Not Updating Inspector
I have created a public class array containing a GameObject, some integers, and some Strings...
[System.Serializable]
public class SpawnerObject
{
public GameObject Object;
public int Weight = 1;
public int WeightValue;
public string Name;
public string Tag;
public int TotalSpawned;
}
public SpawnerObject[] SpawnerObjects;
I also have a method that attempts to load child objects from a specified GameObject (PrefabsFolder) into this class array (see code below). The method is called from the associated editor code button and it is stepping through the 'LoadPrefabsFolder' code but not updating the class array completely...
public void LoadPrefabsFolder()
{
if (PrefabsFolder)
{
// Get count of children in prefab folder
int Prefabs = PrefabsFolder.transform.childCount;
// Update spawner objects array
SpawnerObject[] SpawnerObjects = new SpawnerObject[Prefabs];
// Add each prefab to the spawner objects array
for (int i = 0; i < Prefabs; i++)
{
Transform tObjects = PrefabsFolder.transform.GetChild(i);
GameObject oObject = tObjects.gameObject;
SpawnerObject Prefab = new SpawnerObject();
Prefab.Name = tObjects.name;
Prefab.Tag = tObjects.tag;
Prefab.Object = oObject;
Prefab.TotalSpawned = 0;
Prefab.Weight = 1;
Prefab.WeightValue = 1;
SpawnerObjects[i] = Prefab;
}
}
}
Note that I have removed code that performs various checks (e.g. reports missing objects to the debug log) to simplify the code for you. Can someone tell me what I'm missing/doing wrong here?
ThanX - DJ
Answer by DiGiaCom-Tech · Mar 21, 2015 at 05:04 PM
OK ... I found the problem! In the 'LoadPrefabsFolder' method I was updating the inspector object itself. The fix was to create & load a temporary class object and then set the inspector class object equal to it (the first & last lines of code changed)...
// Update spawner objects array
SpawnerObject[] tSpawnerObjects = new SpawnerObject[Prefabs];
// Add each prefab to the spawner objects array
for (int i = 0; i < Prefabs; i++)
{
Transform tObjects = PrefabsFolder.transform.GetChild(i);
GameObject oObject = tObjects.gameObject;
SpawnerObject tPrefab = new SpawnerObject();
// Set name & tag
tPrefab.Name = tObjects.name;
tPrefab.Tag = tObjects.tag;
// Set object
tPrefab.Object = oObject;
// Set defaults
tPrefab.TotalSpawned = 0;
tPrefab.Weight = 1;
tPrefab.WeightValue = 1;
// Set the prefab as a spawner object
tSpawnerObjects[i] = tPrefab;
}
// Update the inspector
SpawnerObjects = tSpawnerObjects;
How did you add an entry to your array? I am getting this Error NullReferenceException: Object reference not set to an instance of an object
this is the line it is pointing. ButtonPairs[0].SelectButton = button.name;
Your answer
Follow this Question
Related Questions
Editor Script for selecting class property for saving 1 Answer
Get list of all "Action" classes 1 Answer
(Custom WIndow) Access class color from another script and override it 0 Answers
Show a dropdown for C# classes in inspector. 2 Answers
Make custom editor for generic class to apply to all current and future child classes 1 Answer