How is this Instantiate NullReferenceException?
I have gameobject field: public GameObject ChatPrefab;
i set it in Start(): ChatPrefab = Resources.Load("Prefabs/Chat") as GameObject;
instantiate it with the line in Start(): GameObject ChatObject = Instantiate(ChatPrefab, DrawHud.canvas_object.transform);
And when i play it returns: NullReferenceException: Object reference not set to an instance of an object Person.InstantiateChat () (at Assets/Script/Person.cs:30) Person.Start () (at Assets/Script/Person.cs:25)
I don't understand why? The Resources.Load path is correct. If someone can help me understand what's going on that would be greatly appreciated. Thanks
public string Name;
public GameObject ChatPrefab;
public List<DialogueLoader.DialogueScript> PersonsChats = new List<DialogueLoader.DialogueScript>();
public List<GameObject> ChatObjects = new List<GameObject>();
private DialogueLoader dialogueLoader = new DialogueLoader();
// Start is called before the first frame update
void Start()
{
ChatPrefab = Resources.Load("Prefabs/Chat") as GameObject;
dialogueLoader = gameObject.AddComponent<DialogueLoader>();
PersonsChats = dialogueLoader.LoadPersonsScripts(Name);
InstantiateChat();
}
public void InstantiateChat() {
GameObject ChatObject = Instantiate(ChatPrefab, DrawHud.canvas_object.transform);
Chat ChatScript = ChatObject.GetComponent<Chat>();
ChatScript.person = GetComponent<Person>();
ChatScript.Initialize(PersonsChats.Last().script, new Vector3(368, 0, 0));
}
Your answer
Follow this Question
Related Questions
Unity won't Instantiate PLEASE HELP! 1 Answer
NullException Error while instantiating gameobjects 0 Answers
Instantiating GameObject and Adding to list 3 Answers
Destroy Instantiate is not working 0 Answers
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer