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

Bug hello, I can’t understand why it gives an error although I did it from the

Discussion in 'Scripting' started by eanton23, Mar 25, 2023.

  1. eanton23

    eanton23

    Joined:
    Feb 20, 2023
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ItemCollector : MonoBehaviour
    7. {
    8.     private int cherries = 0;
    9.  
    10.     [SerializeField] private Text cherriesText;
    11.  
    12.     [SerializeField] private AudioSource collectionSoundEffect;
    13.  
    14.     private void OnTriggerEnter2D(Collider2D collision)
    15.     {
    16.         if (collision.gameObject.CompareTag("Cherry"))
    17.         {
    18.             collectionSoundEffect.Play();
    19.             Destroy(collision.gameObject);
    20.             cherries++;
    21.             cherriesText.text = "Cherries: " + cherries;
    22.         }
    23.     }
    24. }
     
  2. eanton23

    eanton23

    Joined:
    Feb 20, 2023
    Posts:
    2
    NullReferenceException: ссылка на объект, не подвергшийся воздействию объекта
    ItemCollector.OnTriggerEnter2D (коллизия UnityEngine.Collider2D) (в Assets/Scripts/ItemCollector.cs:21)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947