Sound in Unity is not working! WHY?
The sound was working just fine. The last thing I did before it started failing was implementing tags to my block objects. At one of my scripts, the sound is displayed with PlayClipAtPoint, and before anyone says, it's attached to my main camera. The PlayClipAtPoint sound object appears just fine, but doesn't produce any sound. I'm also not getting any error message. i can't even display the sound on the inspector!
Things I have already tried:
My mute audio button is not pressed
My global volume on the settings is at 1
The sounds and other stuf are all attached to the objects and scripts they need
My audio out of Unity works just fine
Here are the main pieces of my audio related parts of the project so that you can dig into:
Block code / uses PlayClipAtPoint when it's destroyed (here is where I implemented the tags):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
[SerializeField] AudioClip audio;
[SerializeField] GameObject destroyParticlesVFX;
Level level; //level stuff (count blocks and request to update the scene when blocks == 0)
GameStatus gameStatus; // game stuff (keeps track of the score)
// Start is called before the first frame update
void Start()
{
gameStatus = FindObjectOfType<GameStatus>(); // game stuff (keeps track of the score)
if (tag == "Breakable")
{
level = FindObjectOfType<Level>(); //level stuff (count blocks and request to update the scene when blocks == 0)
level.AddBlock(); // add one block to the level counter
}
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D collision)
{
if(tag == "Breakable")
{
DestroyBlock(); // destroying the block procedure
}
}
private void DestroyBlock()
{
TriggerParticlesVFX(); //particle effect
gameStatus.AddPoints(); //add points to score
level.DestroyBlock(); //take one block out of the counter of the level
AudioSource.PlayClipAtPoint(audio, Camera.main.transform.position); //play sound
Destroy(gameObject); // destroy the block game object
}
private void TriggerParticlesVFX()
{
GameObject particle = Instantiate(destroyParticlesVFX, transform.position, transform.rotation);
Destroy(particle, 2f);
}
}
Ball script / Uses sound on collisions and is responsable for destroying the blocks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle;
[SerializeField] float xLauVel = 2f; //launch velocity x
[SerializeField] float yLauVel = 15f; //launch velocity y
[SerializeField] AudioClip[] audios; //collision audio clips
Vector2 pos;
bool stick = true; // stick to the paddle (beggining)
// Start is called before the first frame update
void Start()
{
stick = true;
}
// Update is called once per frame
void Update()
{
StartPosition();
}
void StartPosition() //position of the beggining (stick to the paddle until player clicks the mouse)
{
if (stick == true)
{
pos = new Vector2(paddle.transform.position.x, paddle.transform.position.y + 0.55f);
transform.position = pos;
if (Input.GetMouseButtonUp(0))
{
stick = false;
GetComponent<Rigidbody2D>().velocity = new Vector2(xLauVel, yLauVel);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (!stick)
{
GetComponent<AudioSource>().PlayOneShot(audios[Random.Range(0, audios.Length)]); //if the game has started, display one of the audio clips on collision
}
}
}
Breakable block settings:
Unbreakable block settings:
audio settings:
My game:
Your answer
Follow this Question
Related Questions
I need help with code for audio file. 0 Answers
PAUSE MENU - - If i press "w" and "space change scene.. 0 Answers
raycast mouse detects any object 1 Answer
How do I make a ball bounce in a circle? 0 Answers
Diagonal Wall Jump help 0 Answers