S
S
Step262021-07-31 15:31:08
Unity
Step26, 2021-07-31 15:31:08

What can I do to make the NullReferenceException... error go away?

I am new to Unity. Faced, as I understand it, the most primitive error NullReferenceException: Object reference ... further, I think you know. As I understand it, you need to fill in all the parameters, but they are already filled out. The goal is simple: when the player enters the trigger, the music should start playing. The player has the Player tag, and the object with the trigger has the Music_Trigger tag. The script threw both on the trigger and on the player. I even tried to do it through the sound library, but still, something needs to be filled in. Help if you can, what am I missing?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class musicTrigger : MonoBehaviour
{
    private AudioSource _source;
    public AudioClip[] music_library;

    private void OnTriggerEnter(Collider other)
    {
        if (this.CompareTag("Player") && other.CompareTag("Music_Trigger"))
        {
            if (!_source.isPlaying)
            {
                _source.PlayOneShot(music_library[Random.Range(0, music_library.Length)]);
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
s7on1250, 2021-08-01
@s7on1250

other.CompareTag("Music_Trigger")

Try replacing this with this:
other.gameObject.CompareTag("Music_Trigger")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question