- Home /
Question by
onuregeunaldi1 · Feb 27, 2020 at 09:18 PM ·
c#unity 5nullreferenceexception
While level restarted I get NullReferenceException
Hello, all my problem is while the game is over and start again because of my scoreboard I get the error I pass score values between two scripts. I can not fix it thanks for all
CLASS 1
public class GameOver : MonoBehaviour
{
private int score = 0;
private ScoreBoard scoreBoard;
void Awake()
{
scoreBoard = GameObject.FindObjectOfType<ScoreBoard>();
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Pipe")
{
print("Game is over.");
Invoke("RestartLevel", 1.0f);
}
}
void RestartLevel()
{
score = 0;
SceneManager.LoadScene(0);
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "ScoreUp")
{
print("Score Up");
score = score + 1;
scoreBoard.UpdateScore(score);
}
}
}
CLASS 2
public class ScoreBoard : MonoBehaviour
{
public Text scoreText;
public void UpdateScore(int score)
{
scoreText.text = "Score: " + score.ToString();
}
}
Here is the error that I take:
NullreferenceException : Object reference not set to an instance of an object
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object reference is NULL when IT IS set to an instance of an object? 1 Answer
Distribute terrain in zones 3 Answers
Referencing variables from another script 2 Answers
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer