Search Unity

Question NullreferenceException although already marked by findtag.getcomponent

Discussion in 'C# Job System' started by rafoskiki, Jun 5, 2023.

  1. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    This is the pause text i want to work together with the Death Script,i want it to be able to know if the player has died and then,not be able to bring up the menu.tried else if,and doesnt work

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class PauseMenuTest : MonoBehaviour
    9. {
    10. private bool PlayGM;
    11. public static bool isPaused = false;
    12. public GameObject PauseMenu;
    13. [SerializeField] private GameObject GameMusic;
    14. DeathControl DeathController;
    15. private bool Nospampls = false;
    16.  
    17.  
    18.      void musicplay()
    19.     {
    20.      GameMusic.GetComponent<AudioSource>().Play();
    21.      PlayGM = true;
    22.     } public void Resume()
    23.        {
    24.             isPaused = !isPaused;
    25.        }
    26.      
    27.      void start()
    28.      {
    29.            //Remember the first rule of GameObject.Find():,Do not use GameObject.Find(); xd
    30.           GameMusic = GameObject.FindGameObjectWithTag("MenuMusic");
    31.           GameMusic.GetComponent<AudioSource>();
    32.            Cursor.visible = false;
    33.            PlayGM = true;
    34.            DeathController = GameObject.FindGameObjectWithTag("Death").GetComponent<DeathControl>();
    35.      }
    36.     void Update()
    37.     {
    38.        if (isPaused && DeathController.HasDied == false)
    39.        {
    40.             PauseMenu.SetActive(true);
    41.             Time.timeScale = 0f;
    42.                Cursor.visible = true;
    43.                GameMusic.GetComponent<AudioSource>().Pause();
    44.              PlayGM = false;
    45.        }
    46.        else
    47.        {
    48.           PauseMenu.SetActive(false);
    49.           Time.timeScale = 1f;
    50.           Cursor.visible = false;
    51.  
    52.           if(isPaused == false && PlayGM == false) //i hate that this piece of code works
    53.           {
    54.                musicplay();
    55.           }
    56.          
    57.        }
    58.  
    59.        if(Input.GetKeyDown(KeyCode.Escape))
    60.        {
    61.         isPaused = !isPaused;  
    62.        }
    63.          
    64.           if(DeathController.HasDied == true && Nospampls == false)
    65.           {
    66.                PauseMenu.SetActive(false);
    67.                Time.timeScale = 0f;
    68.                Cursor.visible = true;
    69.                GameMusic.GetComponent<AudioSource>().Pause();
    70.           }
    71.     }
    72.    
    73. }
    74.  
    death Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class DeathControl : MonoBehaviour
    8. {
    9.     // Death menu Activate
    10.  
    11.     public GameObject DeathMenu;
    12.     public GameObject OofText;
    13.     public int RandoNumber;
    14.     // no music fighting
    15.     private GameObject GameMusic;
    16.  
    17.     public bool HasDied = false;
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         GameMusic = GameObject.FindGameObjectWithTag("MenuMusic");
    23.         DeathMenu.SetActive(false);
    24.        
    25.     }
    26.  
    27.     public void DeathStart()
    28.     {
    29.        
    30.         DeathMenu.SetActive(true);
    31.         StartCoroutine(DeathMessage());
    32.         GameMusic.GetComponent<AudioSource>().Pause();
    33.         Time.timeScale = 0f;
    34.         Cursor.visible = true;
    35.         HasDied = true;
    36.     }
    37.  
    38.     public IEnumerator DeathMessage()
    39.     {
    40.         RandoNumber = Random.Range(1,10);
    41.         {
    42.             if (RandoNumber == 1)
    43.             {
    44.                 OofText.GetComponent<Text>().text ="OOF";
    45.             }
    46.             if (RandoNumber == 2)
    47.             {
    48.                 OofText.GetComponent<Text>().text ="Oh,C'mon i would have gotten that!";
    49.             }
    50.             if (RandoNumber == 3)
    51.             {
    52.                 OofText.GetComponent<Text>().text ="Perish!";
    53.             }
    54.             if (RandoNumber == 4)
    55.             {
    56.                 OofText.GetComponent<Text>().text ="R.I.P.";
    57.             }
    58.             if (RandoNumber == 5)
    59.             {
    60.                 OofText.GetComponent<Text>().text ="Really?";
    61.             }
    62.             if (RandoNumber == 6)
    63.             {
    64.                 OofText.GetComponent<Text>().text ="Skill issue";
    65.             }
    66.             if (RandoNumber == 7)
    67.             {
    68.                 OofText.GetComponent<Text>().text ="Can i get an...uhmm number 6?";
    69.             }
    70.             if (RandoNumber == 8)
    71.             {
    72.                 OofText.GetComponent<Text>().text ="H A M B U R G E R";
    73.             }
    74.             if (RandoNumber == 9)
    75.             {
    76.                 OofText.GetComponent<Text>().text ="Snake,are you there?";
    77.             }
    78.             if (RandoNumber == 10)
    79.             {
    80.                 OofText.GetComponent<Text>().text ="Git Gud";
    81.             }
    82.  
    83.             yield return new WaitForSeconds(99999);
    84.         }
    85.     }
    86. }
    87.  
    also Kurt Dekker if you visit this,let me say i respect the grind :)
     
  2. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    Forgot to add ,the error is in line 38 of the pause script,probably the deathcontroller.HasDied Bool
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,970
    The bool won‘t nullref. But the deathcontroller field is null. You have a debugger at your disposal, use it. ;)
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,970
    Oh and … never forget that identifiers are case sensitive in C# (or almost every programming language).

    start() won‘t run unless you call it.
    Start() is what gets called by Unity automatically.
     
  5. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    Right,I got told the same when i asked on the unity discord,the worst thing is that it works perfectly,thanks anyway!