I'm writinh a code t that gives each NPC an id and when yuo talk to the NPC whoes id is 1000, the game is over.
but here is problem
It cause problem that "NullReferenceException: Object reference not set to an instance of an object
"
GameManager.Talk
(at assets/scripts/GameManager.cs:36)
I want to change the Scene 'die' after conversation with NPC
Is other way to solve this problem??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public TalkManager talkManager;
public Text talkText;
public GameObject scanObject;
public GameObject talkPanel;
public bool isAction;
public int talkIndex;
private Title theScene;
private void Start()
{
theScene = FindObjectOfType<Title>();
}
public void Action(GameObject scanObj)
{
scanObject = scanObj;
ObjData objData = scanObject.GetComponent<ObjData>();
Talk(objData.id, objData.isNpc);
talkPanel.SetActive(isAction);
}
void Talk(int id, bool isNpc)
{
string talkData = talkManager.GetTalk(id, talkIndex);
if (talkData == null && id == 1000)
{
isAction = false;
theScene.SceneChange();
return;
}
if(isNpc)
{
talkText.text = talkData;
}
else
{
talkText.text = talkData;
}
isAction = true;
talkIndex++;
}
}
```
public class TalkManager : MonoBehaviour
{
Dictionary<int, string[]> talkData;
private void Awake()
{
talkData = new Dictionary<int, string[]>();
GenerateData();
}
void GenerateData()
{
talkData.Add(1000, new string[] { "Hello?", "Play with me", "Bye" });
}
public string GetTalk(int id, int talkIndex)
{
if (talkIndex == talkData[id].Length)
return null;
else
return talkData[id][talkIndex];
}
}
```
public class Title : MonoBehaviour
{
public void SceneChange()
{
SceneManager.LoadScene("SampleScene");
}
}
I try to use FindObjectOfType
func() to change Scene
I expected to change Scene after conversation with NPC