So I'm programming a soccer crossbar challenge game (This is my first game ever) and I added a script to the crossbar that looks like this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class crossbarscript : MonoBehaviour {
public AudioSource ping;
public static int score;
public Rigidbody rb;
public Text text;
// Use this for initialization
void Start () {
ping = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody>();
score = 0;
}
// Update is called once per frame
public void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Ball")
{
text = GetComponent<Text>();
text.text = "Score: " + score; //This is the line the error is pointing at
ping.Play();
rb.freezeRotation = true;
}
}
}
And in the console, I get this: NullreferenceException: Object reference not set to an instance of an object
What I'm trying to do is make it so that every time the ball hits the crossbar (the object the script is attached to) it adds to the score on the text in the upper left corner. Please let me know if there's a way to fix this or if I should do it another way, thanks.