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

NullReference

Discussion in 'Scripting' started by ZGMGames, Oct 5, 2016.

  1. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    Completed tutorial and tried writing script.. shows this error when played:

    NullReferenceException: Object reference not set to an instance of an object
    Ballcontroll.FixedUpdate () (at Assets/Scripts/Ballcontroll.cs:25)

    the script matches perfectly to one in tutorial. In (console) when I select error message it highlights my Ball "character". What can I do to fix this?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Ballcontroll : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.  
    9.     void start ()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void update()
    15.     {
    16.  
    17.     }
    18.  
    19.     void FixedUpdate ()
    20.     {
    21.         float moveHorizontal = Input.GetAxis("Horizontal");
    22.         float moveVertical =  Input.GetAxis("Vertical");
    23.  
    24.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    25.         rb.AddForce(movement);
    26.     }
    27. }
    28.  
    29.  
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    The object the script is on needs to have a rigidbody attached to get the rigidbody component.

    Actually, looking at it again, it's because you named your functions wrongly.

    It should be "Start" and "Update", with capital letters. All of Unity's MonoBehaviour callback functions start with capital letters.
     
    Kurt-Dekker likes this.
  3. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    Oh goodness..I'll try that on my next script! Thank you.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    And some even have capital letters in their middle, witness FixedUpdate(), etc.!

    The absolute best way when learning how to use Unity's API is when you encounter a particular identifier (such as Start or Update), look it up in the Unity API (easiest way is to google it along with the word Unity), and then take a quick read through what the thing does (if you don't 'get' it all at once, that's fine), then COPY/PASTE the actual identifier from the API over to your code.

    This mechanical process may save you a lot of frustration and it will drive your attention to correct naming early on, which can bite you later, such as with OnTriggerEnter and OnColliderEnter, or is it OnCollisionEnter?? I always forget so I always check when in doubt, especially with the On*** series of callback handlers.
     
  5. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    Thank you guys for the info. I will be sure to google for future reference. Scripting is sure a headache.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148