0

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.

Johan
  • 71,222
  • 23
  • 174
  • 298
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Matt Rowland Jun 21 '16 at 19:49
  • Click on that error and edit your question with the line of code that is causing the error. – Programmer Jun 21 '16 at 19:50
  • @JuanFranciscoPatino No you did not do it. Double click on the error from Unity. It will take you to the line of code the error happened. Post that line of code. Copy the line of code you got the error from. That's it. – Programmer Jun 21 '16 at 20:32
  • @Programmer Oh sorry, either way I commented next to it saying "//This is the line the error is pointing at" right on the code. Ill do as you asked when I can. – Juan Francisco Patino Jun 21 '16 at 20:41
  • @JuanFranciscoPatino I just noticed that. You got an answer from scott and that's the problem. – Programmer Jun 21 '16 at 20:47
  • ciao Juan! your variable that is called "text", really you should change the name. perhaps "scoreDisplay" or just "score". don't use "text" , ideally. – Fattie Jun 21 '16 at 21:29
  • @JoeBlow yep, good idea. Thanks! – Juan Francisco Patino Jun 21 '16 at 21:30

1 Answers1

3

the line

text = GetComponent<Text>();

is unnecessary and is causing your problem. The GameObject you are running this script on does not contain a Text component and is retunning null, this causes text.text to fail on the next line.

You should not be needing to be calling GetComponent<Text>() in your collision code. You already have a public variable, it should likely have been set in the designer by dragging the Text object on to the script. Once set there you don't need to set it in your code.

See from the Roll-A-Ball tutorial "3.3: Displaying the Score and Text" for a example of how you should be using Text in your code to display score.

Scott Chamberlain
  • 116,967
  • 28
  • 260
  • 389
  • So I removed the line like you said, and it gives me the same error pointing to the same line. What did you mean by dragging the Text object onto the script? – Juan Francisco Patino Jun 21 '16 at 21:00
  • Watch the video I linked you to, do what they did with their Text object. If you are just learning Unity for the first time I recommend starting with the very first video in the series and work through the entire series on Roll-a-Ball. Once you finish that one move on to the other tutorial video series they have. – Scott Chamberlain Jun 21 '16 at 21:01
  • Specifically, you need to do the step they do at the [7:47 point](https://youtu.be/bFSLI2cmYYo?t=467) in the video. – Scott Chamberlain Jun 21 '16 at 21:04
  • Thanks! It works exactly how I want it to now, thank you. – Juan Francisco Patino Jun 21 '16 at 21:25
  • Heh Scott - - good one for having to point to the exact second of a video to explain that :) – Fattie Jun 21 '16 at 21:29