- Home /
How do I persist data from a custom inspector for a list? (C#)
Hello! In my project, I have an object with an EnemySpawner script on it. This script stores a list of EnemyInit objects, each of which stores a GameObject for the enemy itself, a Vector3 for its starting position, and a Bool to indicate whether it faces left or right at the start. I have set up a custom inspector for EnemySpawner, which allows me to input all of this data for each enemy, and also choose the number of enemies.
This all seems to be working, until I actually run the game. When I do, the data all resets to the defaults, with no GameObjects in the fields, (0, 0, 0) Vector3s, etc (although, oddly enough, the number of enemies remains correct). I assume this has something to do with me adding and removing elements from the list in the inspector script, but I don't know how else to change the number of enemies from the inspector. Does anyone know what I might try to solve this? Here are the two scripts:
EnemySpawner:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyInit {
public GameObject obj;
public Vector3 start;
public bool faceLeft;
public EnemyInit(GameObject _obj, Vector3 _start, bool _faceLeft) {
obj = _obj;
start = _start;
faceLeft = _faceLeft;
}
public EnemyInit() {
}
}
public class EnemySpawner : MonoBehaviour {
public int enemyNum;
public List<EnemyInit> enemies = new List<EnemyInit>();
// Use this for initialization
void Start () {
for(int i = 0; i < enemies.Count - 1; i++) {
GameObject enemyCurrent = Instantiate ( enemies[i].obj, enemies[i].start, Quaternion.identity ) as GameObject;
if ( enemies[i].faceLeft ) {
enemyCurrent.transform.RotateAround( transform.position, Vector3.up, 180 );
}
}
}
// Update is called once per frame
void Update () {
}
}
EnemySpawnerEditor:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor(typeof(EnemySpawner))]
public class EnemySpawnerEditor : Editor
{
public override void OnInspectorGUI()
{
EnemySpawner myTarget = (EnemySpawner)target;
myTarget.enemyNum = EditorGUILayout.IntSlider ("Enemy Number", myTarget.enemyNum, 1, 10);
while (myTarget.enemyNum > myTarget.enemies.Count) {
myTarget.enemies.Add(new EnemyInit());
}
while (myTarget.enemyNum < myTarget.enemies.Count) {
myTarget.enemies.RemoveAt(myTarget.enemies.Count - 1);
}
EditorGUILayout.Space ();
for (int i = 0; i < myTarget.enemyNum; i++) {
EditorGUILayout.Space ();
EditorGUILayout.LabelField("Enemy " + (i + 1));
myTarget.enemies[i].obj = (GameObject)EditorGUILayout.ObjectField ("Enemy Object", myTarget.enemies[i].obj, typeof(GameObject), false);
myTarget.enemies[i].start = EditorGUILayout.Vector3Field ("Starting Position", myTarget.enemies[i].start);
myTarget.enemies[i].faceLeft = EditorGUILayout.Toggle ("Facing Left", myTarget.enemies[i].faceLeft);
}
}
}
Thank you to anyone who has an idea of what I could to to fix this. I have a feeling that I need to approach the whole thing in a different way, so that I don't have to change the list in the editor script, but I can't think of another way to do what I'm trying to do.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
How do you get a certain index of a List 2 Answers
Unity stops responding when i ask it to execute the same function for the third time 1 Answer