Hi!
I want to play a sound, but it doesn’t work. I can’t hear the sound. Here I give you the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
[SerializeField] private Vector3[] randomPositions = new Vector3[3];
[SerializeField] private AudioClip dead_clip;
public void Restart(int pos)
{
GetComponent<AudioSource>().PlayOneShot(dead_clip, 1);
gameObject.SetActive(true);
transform.localPosition = randomPositions[pos];
}
}
I use Restart void here:
void OnTriggerEnter(Collider col)
{
// checking if the object is a coin
if (col.CompareTag("Coin"))
{
points++;
col.gameObject.SetActive(false);
// checking if all the balls are disabled
bool inactiveAllCoins = true;
for (int i = 0; i < Coin_parent.childCount; i++)
{
if (Coin_parent.GetChild(i).gameObject.activeSelf)
{
inactiveAllCoins = false;
return;
}
}
if (inactiveAllCoins)
{
int randomPos = Random.Range(0, 2);
foreach (Transform coin in Coin_parent)
{
Coin coin_script = coin.gameObject.GetComponent<Coin>();
coin_script.Restart(randomPos);
}
}
}
}
Thanks for reading and answer me if you can. I’d be great for me. Good luck!
Guys, I have finished it. I just modify the Restart void like this, adding a new AudioClip killing_coin_clip:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
// varaibles
[SerializeField] private float speed;
[SerializeField] private GameController gameController;
[SerializeField] private AudioClip killing_coin_clip;
private Vector3 self_rotation = new Vector3();
private int points;
private int record;
Rigidbody m_Rigidbody;
Text Point_txt;
Text Record_txt;
Transform Coin_parent;
AudioSource audioSource;
void Start()
{
// getting components and PlayerPrefbs
m_Rigidbody = GetComponent<Rigidbody>();
Point_txt = GameObject.Find("Points text").GetComponent<Text>();
Record_txt = GameObject.Find("Record text").GetComponent<Text>();
Coin_parent = GameObject.Find("Coins").transform;
audioSource = GetComponent<AudioSource>();
audioSource = GetComponent<AudioSource>();
audioSource = GetComponent<AudioSource>();
record = GetRecord();
}
void Update()
{
// moving the player
/* aca pude haberlo hecho mas entendible. La proxima, puedo hacer un mejor codigo*/
if (Input.GetKey(KeyCode.UpArrow) && Time.timeScale > 0)
{
self_rotation.y = 0;
}
else if (Input.GetKey(KeyCode.DownArrow) && Time.timeScale > 0)
{
self_rotation.y = 180;
}
else if (Input.GetKey(KeyCode.LeftArrow) && Time.timeScale > 0)
{
self_rotation.y = 270;
}
else if (Input.GetKey(KeyCode.RightArrow) && Time.timeScale > 0)
{
self_rotation.y = 90;
}
transform.localEulerAngles = self_rotation;
transform.Translate(new Vector3(0f, 0f, speed) * Time.deltaTime);
// changing to pause state
if (Input.GetKeyDown(KeyCode.Space))
{
gameController.Pause();
}
// setting texts
Point_txt.text = "Points: " + points;
Record_txt.text = "Record: " + record;
// getting record of the player
if(points > record)
{
SetRecord(points);
record = GetRecord();
}
}
void OnCollisionEnter(Collision col)
{
// enemy collision event
if (col.gameObject.tag == "Enemy")
{
gameController.GameOver();
}
}
void OnTriggerEnter(Collider col)
{
// checking if the object is a coin
if (col.CompareTag("Coin"))
{
points++;
audioSource.PlayOneShot(killing_coin_clip, 1);
col.gameObject.SetActive(false);
// checking if all the balls are disabled
bool inactiveAllCoins = true;
for (int i = 0; i < Coin_parent.childCount; i++)
{
if (Coin_parent.GetChild(i).gameObject.activeSelf)
{
inactiveAllCoins = false;
return;
}
}
if (inactiveAllCoins)
{
int randomPos = Random.Range(0, 2);
foreach (Transform coin in Coin_parent)
{
Coin coin_script = coin.gameObject.GetComponent<Coin>();
coin_script.Restart(randomPos);
}
}
}
}
private int GetRecord()
{
return PlayerPrefs.GetInt("Record_int");
}
private void SetRecord(int value)
{
PlayerPrefs.SetInt("Record_int", value);
}
}