Search Unity

Feedback NullReference exception() even when i have everything linked in the hierarchy

Discussion in 'Scripting' started by rafoskiki, May 19, 2023.

  1. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    Player Movement Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PM : MonoBehaviour
    6. {
    7.     private float horizontal;
    8.     private float jumpingPower = 24f;
    9.     private bool isFacingRight = true;
    10.  
    11.  
    12.  
    13.     private bool IsShimmered;
    14.     private bool ShimmerSpam;
    15.  
    16.      private Vector3 respawnPoint;
    17.     public GameObject FallDetector;
    18.     public static Rigidbody2D rb;
    19.     [SerializeField] private Transform groundCheck;
    20.     [SerializeField] private LayerMask groundLayer;
    21.     [SerializeField] private TrailRenderer tr;
    22.     [SerializeField] private float speed = 7f;
    23.  
    24.     private void Start()
    25.     {
    26.         respawnPoint = transform.position;
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.    
    32.         horizontal = Input.GetAxisRaw("Horizontal");
    33.  
    34.         if (Input.GetButtonDown("Jump") && IsGrounded())
    35.         {
    36.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    37.         }
    38.  
    39.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    40.         {
    41.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    42.         }
    43.  
    44.         Flip();
    45.     }
    46.  
    47.     private bool IsGrounded()
    48.     {
    49.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    50.     }
    51. //HERE IS WHERE THE SUPPOSED ERROR IS
    52.      private void FixedUpdate()
    53.     {
    54.         rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    55.     }
    56.  
    57.     private void Flip()
    58.     {
    59.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    60.         {
    61.             Vector3 localScale = transform.localScale;
    62.             isFacingRight = !isFacingRight;
    63.             localScale.x *= -1f;
    64.             transform.localScale = localScale;
    65.         }
    66.     }
    67.  
    68.  
    69.   private void OnTriggerEnter2D(Collider2D collision)
    70.   {
    71.     if(collision.tag == "FallDetector")
    72.     {
    73.         transform.position = respawnPoint;
    74.        
    75.     }
    76.  
    77.     if(collision.tag == "CheckPoint")
    78.     {
    79.         respawnPoint = transform.position;
    80.     }
    81.   }
    82. }
    The script that enables low gravity zones:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shimmeur : MonoBehaviour
    6. {
    7.     private GameObject player;
    8.  
    9.  
    10.     public PM script;
    11.     void Start()
    12.     {
    13.         player = GameObject.FindGameObjectWithTag("Player");
    14.     }
    15.  
    16.         private void OnTriggerEnter2D(Collider2D collision)
    17.         {
    18.             if (collision.tag == "Player")
    19.             {
    20.                 PM.rb.gravityScale = 0.5f;
    21.             }
    22.  
    23.             else
    24.             {
    25.                 PM.rb.gravityScale = 3f;
    26.             }
    27.         }
    28. }
    29.  
    I do know that this question has already been posted several times but after searching for a while i never found the answer i need,any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
  3. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    thats the thing i KNOW what is failing ( it did work before i made the shimmeur script) but i dont know how to fix it
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    That's not step #1.

    Step #1 is identify what is null.

    What is null? You still haven't said and yet you expect someone here to figure it out!

    Again, three steps. It's not hard.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    ALSO: why is line 18 static? That's unnecessary. Access it via normal instance references, like so:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    Your rigidbody is static for some reason and you assign nothing to it. Of course it will be null.

    Don't make it static. Remove that, so it can be serialised in the inspector, and assign it.

    Then, in your
    Shimmeur
    script, get the rigidbody with GetComponent off of the collider, and then do what you need with it.
     
  6. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    I am sorry for my past actions i was pretty frustated,sorry,spiney i will try your advice,ill update you if anything happens,thanks btw