Search Unity

Question Trying to Add Jumping Script with the PlayerController Component

Discussion in 'Scripting' started by CableEight, Jun 8, 2021.

  1. CableEight

    CableEight

    Joined:
    May 27, 2021
    Posts:
    22
    I'm a beginner working on creating what I want to call a "simple" player controller for a 3D Project. I've used Brackey's "THIRD PERSON MOVEMENT in Unity" tutorial to get a functioning camera and movement across the plane with my player object. Now, I'm attempting to add jumping through a separate script using the scripting API's CharacterController.Move section. Which looks like this:

    upload_2021-6-8_13-13-11.png

    The comments mark my understanding of the different sections of code. Now time to get to the main error in my Debug Menu:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerJump.Update () (at Assets/Scripts/PlayerJump.cs:23)

    I don't know exactly what is missing a reference, is it something related to my player object? Or possibly a variable within the code? I know this pertains to the void Update() section, but that's about as far as I can comprehend. Any help is appreciated! :)
     
  2. CableEight

    CableEight

    Joined:
    May 27, 2021
    Posts:
    22
    I just noticed I'm missing the initial declaration "public CollisionFlags Move(Vector3 motion);" but I net pretty similar reference errors, probably due to placement of this declaration. I'm still unsure if this best fits in a void Start() or in the same clump of variables declared before the void Update()
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Fortunately the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    CableEight likes this.
  4. CableEight

    CableEight

    Joined:
    May 27, 2021
    Posts:
    22
    I went through the program testing where different points could have rendered a variable null. I think I found that I needed to first establish that the "controller" variable was referencing the "Character Controller" itself. So I added

    void Start()
    {
    controller = GetComponent<CharacterController>();
    }

    and this started to help, but I also noticed another error in terms of executing the actual code within the if statement. The isGrounded condition isn't really working for my object because it's technically not touching the ground (and I'm unsure why, but when I move the object back down to where it is touching/making contact with the mesh collider of the ground it only stays there when I first hit play, then when I move, it snaps back to Y = 2.4) I've kind of found a half solution and just got rid of the condition completely and opted for a simple limitation condition like transform.y <= 2.4

    Thanks again for your help! Should be almost done with the controller I have after this!
     
  5. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    If you have a character controller capsule, and say a character model (animated), if it's not lined up properly together then the capsule will be the object touching the floor, and the model may appear to float (or be underground or in the wrong place). If you are transforming your character that is appearing to float, to touch the ground then you might find the capsule is underground FYI.