Search Unity

NullReferenceException in my Script

Discussion in '2D' started by fullmetalsoma, Jun 6, 2019.

  1. fullmetalsoma

    fullmetalsoma

    Joined:
    Jun 6, 2019
    Posts:
    2
    So I've been working on a 2D platformer, and in my code it is checking if the collider of the Frog is touching the ground layer, but when I try to run the game i get an error saying that there is an "Object reference not set to an instance of an object" in line 34 "if (coll.IsTouchingLayers(ground))". This is the script for the Frog :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Frog : MonoBehaviour
    6. {
    7.     private Collider2D coll;
    8.     private Rigidbody2D rb;
    9.     private bool facingLeft = true;
    10.  
    11.     [SerializeField] private LayerMask ground;
    12.     [SerializeField] private float leftCap;
    13.     [SerializeField] private float rightCap;
    14.     [SerializeField] private float jumpLength = 10f;
    15.     [SerializeField] private float jumpHeight = 15f;
    16.  
    17.     private void Start()
    18.     {
    19.         GetComponent<Collider2D>();
    20.         GetComponent<Rigidbody2D>();
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         if(facingLeft)
    26.         {
    27.             if(transform.position.x > leftCap)
    28.             {
    29.                 if (transform.localScale.x != 1)
    30.                 {
    31.                     transform.localScale = new Vector3(1, 1);
    32.                 }
    33.                
    34.                 if (coll.IsTouchingLayers(ground))
    35.                 {
    36.                     rb.velocity = new Vector2(-jumpLength, jumpHeight);
    37.                 }
    38.             }
    39.             else
    40.             {
    41.                 facingLeft = false;
    42.             }
    43.         }
    44.  
    45.         else
    46.         {
    47.             if (transform.position.x < rightCap)
    48.             {
    49.                 if (transform.localScale.x != -1)
    50.                 {
    51.                     transform.localScale = new Vector3(-1, 1);
    52.                 }
    53.  
    54.                 if (coll.IsTouchingLayers(ground))
    55.                 {
    56.                     rb.velocity = new Vector2(jumpLength, jumpHeight);
    57.                 }
    58.             }
    59.             else
    60.             {
    61.                 facingLeft = true;
    62.             }
    63.         }
    64.     }
    65. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    This is doing nothing:
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         GetComponent<Collider2D>();
    4.         GetComponent<Rigidbody2D>();
    5.     }
    Your
    coll
    and
    rb
    fields aren't being assigned to.
    They should be assigned like so:
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         coll = GetComponent<Collider2D>();
    4.         rb = GetComponent<Rigidbody2D>();
    5.     }
     
    vakabaka likes this.
  3. fullmetalsoma

    fullmetalsoma

    Joined:
    Jun 6, 2019
    Posts:
    2
    Oh wow. Thank you haha