Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Error In Roll A Ball

Discussion in 'Scripting' started by markuk2016, May 26, 2016.

  1. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    Hi there

    I'm quite new to Unity, and have been working through the YouTube tutorials for the Unity "roll a ball" game.

    Currently i'm up to the stage where you're counting the objects being collected.

    When I run the game, I get the error below, but despite the error, the game runs fine, objects can be picked up and the counter increases;

    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.SetCountText () (at Assets/scripts/PlayerController.cs:49)
    PlayerController.Start () (at Assets/scripts/PlayerController.cs:19)

    Attached is a screenshot.

    I've been through the same section of the tutorial a few times now and cannot for the life of me see where im going wrong.

    Can anyone help?

    Thanks
     

    Attached Files:

  2. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    When you double click on the error, what line does it take you to in your script?
     
  3. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    It goes to line 48
     

    Attached Files:

  4. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    The only thing I can think of that would be causing this error, would be that the CountText is not set for some reason when the SetCountText function is called.
    However, seeing as you are not getting more than one error for this, I assume that this gets fixed latter on.

    I suggest that you put a break point on the SetCountText function, and enter Debug mode.
    If you don't know how to do these, just let me know. :)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You probably missed a drag and drop step in the inspector. Does CountText have a value in the inspector?
     
  6. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    If you could give me some pointers, that would be helpful, thanks
     
  7. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    I'm pretty sure i've done it right, hopefully the attached shows this.
     

    Attached Files:

  8. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    The counter increases as it should when you the sphere hits an object, so i'm really confused as to why an error is coming up. Surely if there's an error with the counter, nothing should happen?
     
  9. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    Any ideas?
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    A very common mistake among new Unity users is that they place more than one instance of a script in the scene, and then forget about one of them.

    On the top of your hierarchy, there's a search bar. If you search for "t:playerController", it'll show you all of the GameObjects in the scene that you have attached player controller to.

    I bet you've got another, rouge PlayerController there, which hasn't had the CountText attached.
     
    TonanBora likes this.
  11. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    I think you mean rogue. If coloring scripts was something you could do that would be awesome! :p

    @markuk2016 Try putting:
    Code (CSharp):
    1. Debug.Log(gameObject.name);
    Right after the code in the error, this will tell you what GameObject's PlayerController script is giving this error.
     
    TonanBora and Baste like this.
  12. markuk2016

    markuk2016

    Joined:
    Apr 21, 2016
    Posts:
    8
    Thanks all, I had the player controller script attached to the main camera. No errors now :)
     
    RavenOfCode and TonanBora like this.