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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Bug while trying to follow the roll a ball tutorial.

Discussion in 'Scripting' started by Mysgym, Apr 7, 2018.

  1. Mysgym

    Mysgym

    Joined:
    Apr 7, 2018
    Posts:
    19
    Hey there,
    so as a beginner in Unity, i started following the Roll a Ball tutorial, But I encountered a bug I couldn't seem to solve while adding the movement forces, I'm pretty sure I made each step correctly and checked my code a few times, here is the bug :
    NullReferenceException: Object reference not set to an instance of an object
    Player_controller.FixedUpdate () (at Assets/Player_controller.cs:19)

    and here is my code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player_controller : MonoBehaviour
    {
    private Rigidbody rb;
    void start ()
    {
    rb = GetComponent<Rigidbody>();
    }
    private void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    //problem line rb.AddForce (movement);

    }


    }
    If anyone is willing to help ?
     
    Last edited: Apr 7, 2018
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Did you add a rigidbody to the object? Also, read the first thread in this forum about using code tags, and always show the line that is causing the error, we don't know what line 19 is. Since there isn't much there, I'm guessing you didn't add a rigidbody.
     
  3. Mysgym

    Mysgym

    Joined:
    Apr 7, 2018
    Posts:
    19
    Sorry about all this, first post.
    Pretty sure I added rigid body, I’ll check later
     
    Last edited: Apr 8, 2018
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    C# is case sensitive, and you wrote 'start' which should be 'Start'. The code to get the component isn't running because of this. :)
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Out of curiosity Mysgym, what editor are you using? Because most editors when you type void start, they will offer code completion so you don't make mistakes like that.
     
  6. Mysgym

    Mysgym

    Joined:
    Apr 7, 2018
    Posts:
    19
    Yayyy, thanks :D
     
  7. Mysgym

    Mysgym

    Joined:
    Apr 7, 2018
    Posts:
    19
    I use monodevelop, it does offer completion but I started coding with HTML JS and I used the bloc note. So I’m not used to autocompletion and prefer writing. Even though it creates those problems...
     
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Those kind of mistakes are hard to see. You are lucky metos5k spotted it. It is so easy to make a typo and hunting it down can be murder. Just for variables, and method names I would highly recommend rethinking that. Not only that, but it can tip you off you are on the wrong track when the option you want doesn't appear. You may have forgotten an include. It's hard enough finding logic errors without looking for typos. As far as the compiler is concerned, you just wrote another method that started with a non-capital "s", so there is no tip off and that's why you were here thinking it was a bug.
     
    Last edited: Apr 8, 2018