F
F
FixMyLife2017-11-02 12:27:21
Audio
FixMyLife, 2017-11-02 12:27:21

Help with audio in unity5?

Need help with sound. When entering the trigger, the sound should play and the object should be removed. The object is deleted, and when playing sound, it throws this error:
There is no 'AudioSource' attached to the "mario right" game object, but a script is trying to access it. You probably need to add an AudioSource to the game object "mario right".
I am attaching the code:

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

[RequireComponent(typeof(AudioSource))]
public class walkscript : MonoBehaviour
{
AudioSource audioSource;
    public AudioClip jumpsound;
    private void Start() // начальная инициализация
        
    {
        audioSource = GetComponent<AudioSource>();
    }
public void OnTriggerEnter2D(Collider2D coll)
    {
      if(coll.gameObject.name == "bonus")
        {
            audioSource.PlayOneShot(jumpsound);
        }
    }

59fae4c11f5e3296434712.jpeg59fae4c820924415034062.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-11-02
@FixMyLife

audioSource audioSource;
make it public and assign a link in the inspector ..
audioSource.PlayOneShot essentially calls you
and the screenshots show that there is no audio source on the object on which walkscript hangs.
well, or hang it on the "mario right" object .. as it is actually written in the error.
Update
And for clarification and understanding,
you are sort of confusing AudioSource and AudioClip
AudioClip is just a link to the audio file itself.
AudioSource is essentially a "player" to which you can "feed" an audio file and a set of settings for playback.
and here's the actual player on mario right and no.
it is on a separate JumpSound - but you are looking for it incorrectly in walkscript Start (), or you forgot to make a link to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question