-3

So i know this is a common problem but the solutions ive seen are not working for me, Sorry if i havent given enough information. Line 31 is where the error happens according to unity. Here is the code

using UnityEngine;
using UnityEngine.UI;

public class Scoring : MonoBehaviour





 public Text Score1;
private int scorePoint1;


// Update is called once per frame
void Start()
{


}
void OnCollisionEnter2D(Collision2D col)
{
    if (col.gameObject.name == ("WallLeft"))
    {
        scorePoint1 += 1;
        Debug.Log("ScorePoint");

    }
}
void Update()
{

    Score1 = GetComponent<Text>();
    Score1.text = "" + scorePoint1.ToString();
}

}

I have tried multiple answers and it seems like everything is connected UI wise (I have tested it with different stuff and it worked) its just this is not working. Im almost it is the scorePoint1 variable that is causing it as i have done it with other variables and it worked but as soon as i switched it out for that it no longer worked. Thanks for the help!

Quick edit: i did a null check as told to on the lines and i confirmed Score1.text = "" + scorePoint1.ToString() ?? string.Empty; this line is what is causing it. Still unsure how to fix it but atleast i know this for certain

  • 1
    Don't just assume which variable is the cause, verify it. Debug through the code. Which line does your IDE stop on when that exception happens? – Broots Waymb Sep 24 '20 at 20:52
  • Try to catch the exception and edit your post to include the exception and stack trace. – mr.coffee Sep 24 '20 at 21:16

1 Answers1

1

From your code any of these expressions can be the cause of the problem:

if (col.gameObject.name == ("WallLeft"))

Score1.text = "" + scorePoint1.ToString();

Debug your code and you will find there's a null check needed in one of those 3 places.

Also as a separate note:

"" + scorePoint1.ToString();

This will potentially allocate 3 strings in memory. First an empty string and then the scorePoint1 as string and then third string where the 2 values are concatenated together. This is very bad for performance especially in a game or in a loop. If you want a string that is never null, do this instead. That will be 3 times more memory efficient:

scorePoint1?.ToString() ?? string.Empty;

Ad1:

Score1.text = "" + scorePoint1.ToString() ?? string.Empty;

The ?? is null coalescing. = If left side is null, use right side instead.

But that is not your problem, you can't call ToString() on a null object. In my example I use the ?. null-conditional. = If left side is null, don't call the method/property after ?. and return null.

This is what you want, it will never fail even if there's null.

Score1.text = scorePoint1?.ToString() ?? string.Empty;

So you can see if scorePoint1 is null, ToString() is never called and prevents your error. The expression scorePoint1?.ToString() returns null, which is handled by the null coalesce and gets replaced by string.Empty.

NIKER
  • 464
  • 2
  • 9
  • Thanks i did that and did the null checks and on the edit to my post i explain which one it was (spoiler it was this line Score1.text = "" + scorePoint1.ToString() ?? string.Empty; ) if you know any ways to fix it please let me know. – PongGivesMePain Sep 24 '20 at 21:28
  • Posted update and further explanation what it exactly does. – NIKER Sep 24 '20 at 21:49