Search Unity

Question Object reference not set to an instance of an Object C#

Discussion in 'Scripting' started by unity_A7FWZ-hgfcZv3w, Sep 26, 2022.

  1. unity_A7FWZ-hgfcZv3w

    unity_A7FWZ-hgfcZv3w

    Joined:
    Sep 7, 2022
    Posts:
    2
    Hi sorry for any misspelled word english aint my first language...
    If anyone could give me any info to help me learn the logic behind it i am totally lost since im pretty new to all of that.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class stairsVar : MonoBehaviour
    6. {
    7.     private PlayerMovement playerController;
    8.     private HeadBobController headBob;
    9.     private void Awake()
    10.     {
    11.         playerController = GetComponent<PlayerMovement>();
    12.         headBob = GetComponent<HeadBobController>();
    13.     }
    14.     private void OnTriggerEnter(Collider collider)
    15.     {
    16.         Debug.Log(collider);
    17.         headBob.Able();
    18.         playerController.EnterStairs();
    19.     }
    20.     private void OnTriggerExit(Collider collider)
    21.     {
    22.         Debug.Log(collider);
    23.         headBob.Disable();
    24.         playerController.ExitStairs();
    25.     }
    26. }
    and here is the functions from HeadBob

    Code (CSharp):
    1.     public void Able()
    2.     {
    3.         _enable = true;
    4.         Debug.Log("ye");
    5.     }
    6.     public void Disable()
    7.     {
    8.         _enable = false;
    9.         Debug.Log("ye");
    10.     }
    and here from PlayerMovement
    Code (CSharp):
    1.     public float walkSpeed = 4f;
    2.     public float currentSpeed;
    3.     public float crouchedSpeed = 2f;
    4.     public float runSpeed = 8f;
    5.     public float gravity = -28f;
    6.  
    7.  
    8. [Header("onStairsStat")]
    9.     [SerializeField] float newGravity = -3000f;
    10.     [HideInInspector] float oldGravity = -28f;
    11.     [SerializeField] float newRunSpeed = 6f;
    12.     [HideInInspector] float oldRunSpeed = 8f;
    13.     [SerializeField] float newWalkSpeed = 3f;
    14.     [HideInInspector] float oldWalkSpeed = 4f;
    15.     [SerializeField] float newCrouchedSpeed = 1f;
    16.     [HideInInspector] float oldCrouchedSpeed = 2f;
    17.  
    18. public void ExitStairs()
    19.     {
    20.         gravity = oldGravity;
    21.         runSpeed = oldRunSpeed;
    22.         walkSpeed = oldWalkSpeed;
    23.         crouchedSpeed = oldCrouchedSpeed;
    24.         Debug.Log("ye");
    25.     }
    26.     public void EnterStairs()
    27.     {
    28.         gravity = newGravity;
    29.         runSpeed = newRunSpeed;
    30.         walkSpeed = newWalkSpeed;
    31.         crouchedSpeed = newCrouchedSpeed;
    32.         Debug.Log("ye");
    33.     }
    Here is the actual error code
    FPP (UnityEngine.CharacterController)
    UnityEngine.Debug:Log (object)
    StairsVar:OnTriggerExit (UnityEngine.Collider) (at Assets/Scripts/StairsVar.cs:22)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
  3. unity_A7FWZ-hgfcZv3w

    unity_A7FWZ-hgfcZv3w

    Joined:
    Sep 7, 2022
    Posts:
    2
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    > how to fix it any ideas

    How is it supposed to be assigned? If you don't know, go back to the tutorial where you got this because you haven't done the tutorial fully.

    Pay attention to Step #2 below.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.


    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!