Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question