0

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

user
  • 1
  • 3
  • Since `SceneManager.LoadScene("SampleScene");` is a call to a `static` method ... why do you need the `Title` component at all? – derHugo Nov 08 '22 at 10:57
  • Did you check if 'theScene' variable is not null? Seem that on start when you call FindObjectOfType() function, could be returning null as result. – Andoni Rivera Nov 08 '22 at 10:58
  • @derHugo i can't understand exactly, it means that doesn't need it? – user Nov 08 '22 at 11:16
  • @Andoni Rivera Where do I add it to check? – user Nov 08 '22 at 11:22
  • In line 36 you are calling to the method SceneChange. The problem seem to be that in Start function the object named 'theScene' is null, and you can't call to a method if the object is null. – Andoni Rivera Nov 08 '22 at 11:28

0 Answers0