Search Unity

NullReferenceException: Object reference not set to an instance of an object GameManagement.EndGame

Discussion in 'Editor & General Support' started by thefrogeitan, Oct 20, 2019.

  1. thefrogeitan

    thefrogeitan

    Joined:
    Oct 20, 2019
    Posts:
    7
    I begin to learn unity and i have problem in my first game
    i try to save the coins i get with a playerprefs and how match time you lose and the scripts compile
    but in the console its right me
    NullReferenceException: Object reference not set to an instance of an object
    GameManagement.EndGame () (at Assets/Scripts/GameManagement.cs:25)
    PlayerCollision.OnCollisionEnter (UnityEngine.Collision collisioninfo) (at Assets/Scripts/PlayerCollision.cs:17)
    the 3 xcripts are downstairs
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. using UnityEngine;
    3.  
    4. public class GameManagement : MonoBehaviour
    5. {
    6.     bool GameHasEnded = false;
    7.     public float restartDelay = 2f;
    8.     public GameObject completeLevelUI;
    9.     public coinText ct;
    10.     public int LoseCounter;
    11.     public EndTrigger et;
    12.  
    13.    
    14.  
    15.     public void EndGame()
    16.     {
    17.         if (GameHasEnded == false )
    18.         {
    19.            
    20.             LoseCounter++;
    21.            
    22.                  
    23.             Invoke("Restart", restartDelay);
    24.             GameHasEnded = true;
    25.             ct.changetext();
    26.            
    27.         }
    28.     }
    29.  
    30.     public void Restart()
    31.     {
    32.         GameHasEnded = false;
    33.         savedata();
    34.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    35.         loaddata();
    36.     }
    37.     public void CompleteLVL()
    38.     {
    39.         completeLevelUI.SetActive(true);
    40.     }
    41.     public void savedata()
    42.     {
    43.         PlayerPrefs.SetInt("coinamount", ct.coinAmount);
    44.         PlayerPrefs.SetInt("losecounter", LoseCounter);
    45.         PlayerPrefs.SetInt("level1", et.lvone);
    46.         PlayerPrefs.SetInt("level2", et.lvtow);
    47.         PlayerPrefs.SetInt("level3", et.lvthree);
    48.         PlayerPrefs.Save();
    49.     }
    50.     public void loaddata()
    51.     {
    52.         ct.coinAmount = PlayerPrefs.GetInt("coinamount");
    53.         LoseCounter = PlayerPrefs.GetInt("losecounter");
    54.         et.lvone = PlayerPrefs.GetInt("level1");
    55.         et.lvtow = PlayerPrefs.GetInt("level2");
    56.         et.lvthree = PlayerPrefs.GetInt("level3");
    57.     }
    58.  
    59.  
    60.  
    61. }
    62.  
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4.  
    5. public class coinText : MonoBehaviour
    6. {
    7.      public int coinAmount;
    8.  
    9.    
    10.     public Text text;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         text = GetComponent<Text>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         changetext();
    23.     }
    24.     public void changetext()
    25.     {
    26.        
    27.         text.text = coinAmount.ToString() + " coins";
    28.     }
    29. }
    30.  








    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3.  
    4. public class PlayerCollision : MonoBehaviour
    5. {
    6.     public PlayerMovement PM;
    7.    
    8.    
    9.     public GameManagement gm;
    10.     void OnCollisionEnter (Collision collisioninfo)
    11.     {
    12.  
    13.         //when the player collision object tag "obstcle" the player movement Unenabled
    14.         if (collisioninfo.collider.tag == "obstcle")
    15.         {
    16.             PM.enabled = false;
    17.             FindObjectOfType<GameManagement>().EndGame();
    18.            
    19.  
    20.         }
    21.        
    22.        
    23.  
    24.  
    25.  
    26.     }
    27.    
    28.  
    29.     }
    30.  
     
  2. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    Well it's telling you exactly where and what the problem is.
    In line 25 there's only one reference which could be null, "ct". So either assign ct in the editor or in script.
     
  3. thefrogeitan

    thefrogeitan

    Joined:
    Oct 20, 2019
    Posts:
    7
    I realy new in unity can you write me that in script?