- Home /
How do i edit a constructor array in the inspector?
Im kinda new to coding and I like making things myself. But I dont know how to edit the properties from a constructor in an array in the inspector. I can drag and drop a class object but not edit the properties. For example: In my NPC constructor is a id of type int. I basically want to edit the id property in the inspector for one of the NPC constuctors in the array. I can only drag and drop a NPC class which i dont want to do.
My Spawner:
public class LoadNPCAndObstacles : MonoBehaviour
{
public Transform[] ObstacleSpawns;
public Transform[] NPCSpawns;
[SerializeField]
public NPC[] Npcs;
void Start()
{
Npcs = new NPC[NPCSpawns.Length];
}
void Update()
{
}
}
My Constructor:
public class NPC : Obstacle
{
public bool HasPath { get; set; }
public GameObject Path { get; set; }
public NPC(float hp, int id, Vector3 position, Quaternion rotation,GameObject prefab, bool hasPath, GameObject path) : base(hp, id, position, rotation, prefab)
{
HP = hp;
ID = id;
Position = position;
Rotation = rotation;
HasPath = hasPath;
Path = path;
Prefab = prefab;
}
public void CreateNPC()
{
Instantiate(this.Prefab, this.Position, this.Rotation);
}
}
Answer by Llama_w_2Ls · Apr 03, 2021 at 05:28 PM
Have you added the attribute System.Serializable
at the top of your script? It should look like this:
[System.Serializable]
public class NPC : Obstacle
{
public bool HasPath { get; set; }
public GameObject Path { get; set; }
public NPC(float hp, int id, Vector3 position, Quaternion rotation,GameObject prefab, bool hasPath, GameObject path) : base(hp, id, position, rotation, prefab)
{
HP = hp;
ID = id;
Position = position;
Rotation = rotation;
HasPath = hasPath;
Path = path;
Prefab = prefab;
}
public void CreateNPC()
{
Instantiate(this.Prefab, this.Position, this.Rotation);
}
}
Yea, still i can only drag in a class and not change the properties
Is there a need to make them properties instead of fields? You aren't using the get; set; accessors any differently, so you might as well change public bool HasPath { get; set; }
to just public bool HasPath;
and it should show up in the inspector.