- Home /
NPC Spawning after unlocking
So, i am building something which involves building your town. Not like in Age of Empires, but your town kind of ‘evolves’ after unlocking a specific object.
I have a map where an NPC walks around. He’s lost in the forest. When you approach the NPC, you can talk to him and he says thanks for helping. Now you’ve unlocked the NPC, which means that the NPC is now walking in your town and lives in a house.
Also, the houses spawn with the NPC in town.
I have been thinking about how to build this and did find a solution to it, but it seems bad practise.
What if i store the NPC game object inside a list when the NPC is saved and spawn it in town at a given spawnpoint? Is this bad practise or not?
public class NPC$$anonymous$$anager : $$anonymous$$onoBehaviour {
[SerializeField] List<NPC> unlockedNPCs;
// Use this for initialization
void Start () {
unlockedNPCs.ForEach(npc => SpawnVillager(npc.id));
}
// Update is called once per frame
void Update () {
}
public void UnlockVillager(NPC npc)
{
unlockedNPCs.Add(npc);
SpawnVillager(npc.id);
print("You unlocked the " + npc.name);
}
void SpawnVillager(int id)
{
GameObject gameObj = Resources.Load("NPC/villager_" + id) as GameObject;
GameObject npcObj = Instantiate(gameObj, new Vector3(0, 0, 0), transform.rotation, null);
npcObj.GetComponent<NPC>().isLost = false;
}
}
Answer by CommodoreClutch · Sep 05, 2018 at 04:50 PM
It would be nice if someone could help me out with this, i am kind of stuck on what's efficient and what's not.