-4

You don't have to read all the code, my question is a bit further down. Thank you for helping :s

public GameObject CanvasGame;
public GameObject canvasRestart;

public GameObject WinTxt;
public GameObject LoseTxt;

public GameObject scoreScripte;
public GameObject puckScript;

public GameObject playerMovement1;
public GameObject playerMovement2;

public void ShowRestartCanvas(bool didP1win)
{
    Time.timeScale = 0;
    CanvasGame.SetActive(false);
    canvasRestart.SetActive(true);

    if (didP1win)
    {
        WinTxt.SetActive(true);
        LoseTxt.SetActive(false);
    }
    else
    {
        WinTxt.SetActive(false);
        LoseTxt.SetActive(true);
    }
}

using UnityEngine.UI;
using UnityEngine;

public class ScoreScript : MonoBehaviour
{
    public enum Score
    {
        P1Score, P2Score
    }

    public Text P1ScoreTxt, P2ScoreTxt;

    public UIManager uiManager;
    public int MaxScore;

    #region Scores
    private int p1Score, p2Score;

    private int P1Score
    {
        get { return p1Score; }
        set
        {
            p1Score = value;
            if (value == MaxScore)
                uiManager.ShowRestartCanvas(true);
        }
    }

   private int P2Score
    {
        get { return p2Score; }
        set
        {
            p2Score = value;
            if (value == MaxScore)
                uiManager.ShowRestartCanvas(false); 
        }
    }
    #endregion

public void Increment(Score whichScore)
{
    if (whichScore == Score.P1Score)
    {
        P1ScoreTxt.text = (++P1Score).ToString();
    }
    else
    {
        P2ScoreTxt.text = (++P2Score).ToString();
    }
}

}

That first bunch of code is my UI Manager the second one is my Score Script. The error is:

NullReferenceException: Object reference not set to an instance of an object ScoreScript.set_P1Score (System.Int32 value) (at Assets/Scripts/ScoreScript.cs:26)

The UI does not pop up when the MaxScore value is met.

The lines that the error is referencing to are:

uiManager.ShowRestartCanvas(false);
uiManager.ShowRestartCanvas(true);

I tried but couldn't find the solution which is prob are easy one, I'm grateful for any help.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 1
    `uimanager` is presumably `null`. – ProgrammingLlama Feb 03 '21 at 06:04
  • Did you set the value of `uiManager` variable anywhere before calling `uiManager. ShowRestartCanvas()`? Looks like `uiManager` is null. – Serg Feb 03 '21 at 06:04
  • 1
    _"You don't have to read all the code"_ probably implies you should post the minimum sample necessary to reproduce the problem per guidelines. – Ian Mercer Feb 03 '21 at 06:10
  • I thought that this could bring a better understanding, but I appriciate ur comment and will respect it for later questions. – punpun willi Feb 03 '21 at 06:17
  • UIManager is the class name of the UIManager, I intended to call the function in this class. – punpun willi Feb 03 '21 at 06:21
  • If `UIManager` is a class that extends Unity's Object class somewhere down the line. You have to either set it in the inspector (if it's in the editor scene before runtime) or you have to set it some where in the code. Nothing new here. – Omar Abdel Bari Feb 03 '21 at 06:29
  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – SᴇM Feb 03 '21 at 06:34
  • Thanks I was looking for that duplicate – Omar Abdel Bari Feb 03 '21 at 06:35
  • please use the correct tags ... [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long deprecated by now ... your code is clearly `c#` – derHugo Mar 22 '21 at 11:47

2 Answers2

0

You declare UiManager:

public UIManager uiManager;

This declares a variable of type UIManager, which is empty (null), you need to initialize the class before using it, with something like this:

public UIManager uiManager = new UIManager(...); 

The exact signature of the constructor, the values that go instead of ... in the example above, if any, depend on the class UIManager.

Julio Cachay
  • 780
  • 5
  • 10
  • It does not show but the first code is called UIManager --public class UIManager : MonoBehaviour-- I thought im referencing to this class with --public UIManager uiManager;-- So that I can call the ShowRestartCanvas function in UIManager – punpun willi Feb 03 '21 at 06:15
0

I get it now. UIManager is a component, therefore, you don't initialize it with "new" as we do with regular classes, instead, In the inspector, you will have a field for the UIManager variable you created.

You need to drag the component of type UIManager into that field.

That will initialize the variable.

See this link to the Unity manual

Julio Cachay
  • 780
  • 5
  • 10