- Home /
Question by
rahulkuhadjain · Jul 11, 2020 at 06:08 PM ·
audioaudiosourcetags
Audio not working With Compare Tag
Hello, ( I am new to Unity )
I have 3-4 different prefabs with tags ( Good & Bad )
On Mouse click the game objects get destroyed and scores are awarded.
I want to add different Sounds to tags . One type for Good and One type for Bad Tags.
I have used the following Code , Let me know what's wrong and how to solve it.
TIA
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Target : MonoBehaviour {
private Rigidbody targetRb;
public AudioSource source;
public AudioClip eathit;
public AudioClip skullhit;
// Start is called before the first frame update
void Start()
{
source = GetComponent<AudioSource>();
targetRb = GetComponent<Rigidbody>();
// using this so the good and bad objects can pass eachother without colliding
Physics.IgnoreLayerCollision(10, 10);
// game beginning audio
source.Play();
}
// Update is called once per frame
void Update()
{
}
private void OnMouseDown()
{
if (gameObject.CompareTag("Bad"))
{
Debug.Log("BAD");
// debug is working but audio is not playing
source.PlayOneShot(skullhit, 1);
}
if (gameObject.CompareTag("Good"))
{
Debug.Log("GOOD");
source.PlayOneShot(eathit, 1);
}
}
private void OnTriggerEnter(Collider other)
{
if (!gameObject.CompareTag("Bad"))
{
gamemanager.GameOver();
}
Destroy(gameObject);
}
}
Comment