Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

I'm trying to display the victor on my Game Over Scene

Discussion in 'Scripting' started by Skin-N-Bone, Aug 4, 2019.

  1. Skin-N-Bone

    Skin-N-Bone

    Joined:
    Dec 29, 2018
    Posts:
    5
    Okay so I'm trying to build a Pong clone. This is my first solo project outside of tutorials so it goes without saying that I am very inexperienced. Everything was going well (or so I thought) until I hit this point.

    The title explains my initial problem but now I'm starting to doubt every decision I have made with the logic and flow of my code. There are 3 classes that are working together which I'm going to open up to you guys for a good old critique if you willing and up for the task.

    I'm getting the following error when player2's score reaches 2:
    NullReferenceException: Object reference not set to an instance of an object
    GameManagement.UpdateScore2 () (at Assets/Scripts/GameManagement.cs:37)
    Ball.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Ball.cs:27)






    Code (CSharp):
    1. public class Ball : MonoBehaviour
    2. {
    3.     [SerializeField] float xPush = -10f, yPush = 0f;
    4.     Rigidbody2D myRididbody;
    5.     GameManagement myGame;
    6.  
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         myGame = FindObjectOfType<GameManagement>();
    12.         myRididbody = GetComponent<Rigidbody2D>();
    13.         myRididbody.velocity = new Vector2(xPush, yPush);
    14.     }
    15.  
    16.     private void OnCollisionEnter2D(Collision2D collision)
    17.     {
    18.         myRididbody.velocity = new Vector2(myRididbody.velocity.x , Random.Range(-4, 4));
    19.       //  Debug.Log(myRididbody.velocity);
    20.  
    21.         if (collision.gameObject.tag == "ScoreWallLeft")
    22.         {
    23.             myGame.UpdateScore2();
    24.             Debug.Log("Collision with tag detected");
    25.         }
    26.         if (collision.gameObject.tag == "ScoreWallRight")
    27.         {
    28.             myGame.UpdateScore1();
    29.             Debug.Log("Collision with tag detected");
    30.         }
    31.     }
    32. }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. public class GameManagement : MonoBehaviour
    9. {
    10.  
    11.     [SerializeField] TextMeshProUGUI Player1ScoreText;
    12.     [SerializeField] TextMeshProUGUI Player2ScoreText;
    13.     Victory victor;
    14.     int player1Score = 0;
    15.     int player2Score = 0;
    16.  
    17.  
    18.     public  void OnButtonPressed(int sceneID)
    19.     {
    20.         SceneManager.LoadScene(sceneID);
    21.     }
    22.  
    23.     public void UpdateScore1()  // udates score for player 1
    24.     {
    25.         player1Score++;
    26.         Debug.Log(player1Score);
    27.         Player1ScoreText.text = player1Score.ToString();
    28.     }
    29.     public void UpdateScore2()   // updates score for player 2
    30.     {
    31.         player2Score++;
    32.         Player2ScoreText.text = player2Score.ToString();
    33.  
    34.         if (player2Score >= 2)
    35.         {
    36.            victor.SetWinner("Player 2");
    37.            EndRound();
    38.  
    39.         }
    40.     }
    41.  
    42.     public void EndRound()
    43.     {
    44.             SceneManager.LoadScene("Game Over");
    45.     }
    46.  

    Code (CSharp):
    1.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Victory : MonoBehaviour
    7. {
    8.    //TextMeshProUGUI VictoryText;
    9.  
    10.     string winner = "";
    11.  
    12.     private void Awake()
    13.     {
    14.         int scriptCount = FindObjectsOfType<Victory>().Length;
    15.         if (scriptCount > 1)
    16.         {
    17.             Destroy(gameObject);
    18.         }
    19.         else
    20.         {
    21.             DontDestroyOnLoad(gameObject);
    22.             DisplayWinner();
    23.         }
    24.     }
    25.  
    26.     public void SetWinner(string player)
    27.     {
    28.         winner = player;
    29.     }
    30.  
    31.     public void DisplayWinner()
    32.     {
    33.        gameObject.GetComponent<TextMeshProUGUI>().text = winner;
    34.     }
    35. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    7,690
    Where do you set the value for the "victor" variable in GameManagment?
     
  3. Skin-N-Bone

    Skin-N-Bone

    Joined:
    Dec 29, 2018
    Posts:
    5
    In the GameManagement class I cache the Victory class:

    Code (CSharp):
    1. Victory victor;
    and then set it here:
    Code (CSharp):
    1. public void UpdateScore2()   // updates score for player 2
    2.     {
    3.         player2Score++;
    4.         Player2ScoreText.text = player2Score.ToString();
    5.  
    6.         if (player2Score >= 2)
    7.         {
    8.            victor.SetWinner("Player 2");
    9.            EndRound();
    10.  
    11.         }
    12.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    7,690
    The code above never sets victory to any value other than its default value of null.

    Before you can call an instance method like .SetWinner() on victory, you have to first set victory to reference a valid Victory object.
     
  5. Skin-N-Bone

    Skin-N-Bone

    Joined:
    Dec 29, 2018
    Posts:
    5
    Thanks for getting back to me. You helped me fix it.
     
unityunity