- Home /
Trying to have my Editor Script save the changed settings when I hit Play
As the title mostly says, I'm trying to get my prefab that uses a script that is linked to an editor script save what I changed when I hit the play button. I was able to save the changes made to the slider, however, the list that holds all the objects that are generated does not save. Below are both my codes for each script.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(EnemySpawner))]
public class EnemySpawnerEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EnemySpawner spawner = (EnemySpawner)target;
GUILayout.Space(15);
GUILayout.Label("How many of the Enemy do you want to spawn");
EditorGUI.BeginChangeCheck();
int amount = (int)EditorGUILayout.Slider("Amount", spawner.Amount, 0, 10);
if(EditorGUI.EndChangeCheck())
{
if(spawner.Amount < amount)
{
spawner.Amount = amount;
spawner.Generate();
//spawner.points.Clear();
GameObject point = GameObject.Find("/Spawn Point(Clone)");
point.transform.SetParent(spawner.transform);
}
else if(spawner.Amount > amount)
{
spawner.Amount = amount;
spawner.Degenerate();
}
Undo.RecordObject(spawner, "How many entities do you want to spawn");
}
}
}
////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[Tooltip("The Enemy you want to spawn")]
public EnemyDB Enemy;
//[Range(0, 10)]
//[Tooltip("How many of the Enemy do you want to spawn")]
public int Amount;
[Tooltip("What rarity type do you want the spawing Enemy to be")]
public Type EnemyType;
[HideInInspector] public GameObject entity;
[HideInInspector] public GameObject point;
public List<GameObject> points = new List<GameObject>();
void Start()
{
switch(Enemy)
{
case EnemyDB.Slime:
spawn("Slime");
this.gameObject.SetActive(false);
break;
}
}
public void spawn(string name)
{
entity = Resources.Load<GameObject>("Enemies/" + name);
for(int i = 0; i < Amount; i++)
{
Vector3 pos = points[i].transform.position;
entity.GetComponent<Enemy>().num += i;
Instantiate(entity, pos, Quaternion.identity);
entity.GetComponent<Enemy>().num = 0;
}
}
public void Generate()
{
point = Resources.Load<GameObject>("Tools/Spawn Point");
Vector3 pos = new Vector3(0, 0, 0);
pos.x = Random.Range(this.transform.position.x - 1, this.transform.position.x + 1);
pos.y = Random.Range(this.transform.position.y - 1, this.transform.position.y + 1);
points.Add(Instantiate(point, pos, Quaternion.identity));
}
public void Degenerate()
{
point = GameObject.Find("/" + this.gameObject.name + "/Spawn Point(Clone)");
GameObject.DestroyImmediate(point.gameObject);
points.Remove(point);
}
}
Any help will be appreciated.
Comment
Your answer
Follow this Question
Related Questions
I want to access objects in my scene with a prefab when it spawns. 1 Answer
How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer
How to modify prefab permanently via script. 0 Answers
A node in a childnode? 1 Answer
Can I force z position to match prefab when it's dragged onto scene? 1 Answer