I am trying to add a sound into my game that whenever the player moves over a certain space it plays a crunch sound. I have created the AudioSource file and a .OGG file for the sound.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpaceBlue : MonoBehaviour { public Transform spaceNext; public AudioSource stepOnObject; public AudioClip stepOnSound; private void Start() { stepOnObject.clip = stepOnSound; stepOnObject.enabled = true; } private void OnTriggerEnter(Collider other) { if (other.tag == "Player") { stepOnObject.Play(); if (BoardScript.diceValue > 0) { BoardScript.diceValue -= 1; Debug.Log("The dice are now " +BoardScript.diceValue); other.transform.LookAt(spaceNext); } } } } 

I have included the source and clip to my game object and i have tried it both with and without "Play on wake" selected.

Whenever the play walks over the player walks over the object i get a warning in the unity engine saying that the source is disabled.

Any help is appreciated :)

5

2 Answers

I had a similar problem and I fixed it by changing the location of the Play() call to after calling Destroy(GameObject). I would recommend trying moving the call to Play() to the end of the function, or trying Invoke("stepOnObject.Play", 0.5f); to ensure it gets called.

Otherwise, make sure its checkbox is ticked, and that the AudioSource actually has a AudioClip attached.

If you have any piece of code in some other script that Destroys this game object or makes SetActive false, then the best way to solve this problem will be to delay that piece of code by some time using a Coroutine.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.