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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Hey! I need help with my Colliable script! Plz help

Discussion in 'Getting Started' started by TheElectricWolf, Nov 16, 2022.

  1. TheElectricWolf

    TheElectricWolf

    Joined:
    Aug 12, 2021
    Posts:
    10
    I got this script that's suppose to give me a log when the player collides with the gameobject with this script but it only gives me this error when starting the game.
    it shows no error in Visual Studio though.


    public class Collidable : MonoBehaviour
    {
    public ContactFilter2D filter;
    private BoxCollider2D boxCollider;
    private Collider2D[] hits = new Collider2D[10];



    protected virtual void start()
    {
    boxCollider = GetComponent<BoxCollider2D>();
    }

    protected virtual void Update() {

    boxCollider.OverlapCollider(filter, hits);
    for (int i = 0; i < hits.Length; i++)
    {
    if (hits == null)
    continue;


    Debug.Log(hits.name);

    hits = null;

    }



    }


    }

    View attachment 1151764
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Welcome to the forums. When sharing code, ensure you use code tags to allow the code's formatting to remain intact, along with syntax highlighting that makes spotting issues much easier.

    In this case, though, I can see that your issue is likely caused by your Start() method being misnamed "start". Capitalization matters in C#, so ensure you're entering things as intended and following conventions. "Start" gets called automatically by Unity, while "start" would not. That means that when you got to the Update being executed, your boxCollider variable would remain unset and is what's causing the errors to flood your console as you try to access it 60 times a second.

    If you're using an IDE like Visual Studio, VS Code, Jetbrains Rider, or even Script Inspector from the Asset Store, your text editor may even spot issues like this for you:
    upload_2022-11-16_11-57-42.png
     
    JoeStrout likes this.
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    1) Use CODE tags.
    2) Watch your spelling and grammar when typing code, slightest typo and it won't work.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You don't actually mention the error. It's now just a guessing game for us. ;)

    Your code isn't good.

    If you are using an array, it's length is always whatever you set, it's not how many results are in it. In that case, you need to use the value returned from the query i.e. "var hitCount = boxCollider.OverlapCollider(filter, hits)" and iterate this many of the array. You also access an array with "hits". Note, that if you get 10 hits then next time, you'll see have those 10 hits in the array, they are not set to NULL.

    Here's a much better way of doing it using a list. This list will expand automatically so you don't need to pre-create a certain size:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Collidable : MonoBehaviour
    6. {
    7.     public ContactFilter2D filter;
    8.     private BoxCollider2D boxCollider;
    9.     private List<Collider2D> hits = new List<Collider2D>();
    10.  
    11.     void Start()
    12.     {
    13.         boxCollider = GetComponent<BoxCollider2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         boxCollider.OverlapCollider(filter, hits);
    19.  
    20.         for (int i = 0; i < hits.Count; i++)
    21.         {
    22.             Debug.Log(hits[i].name);        
    23.         }
    24.     }
    25. }
    Note: As above, please used code-tags when posting code.
     
  5. TheElectricWolf

    TheElectricWolf

    Joined:
    Aug 12, 2021
    Posts:
    10
    yea sorry for some reason the attachment didn't work, the error said "NullReferenceExeption: Object Reference is not set for the instance of the object" Thanks anyways! I'm very new to coding and in development of my first game so thanks for the help!
     
  6. TheElectricWolf

    TheElectricWolf

    Joined:
    Aug 12, 2021
    Posts:
    10
    Got it! I'm very new to coding and in development of my first game so thanks for the help!
     
    Schneider21 likes this.