- Home /
I found the answer!
Audio script not working,Audio Not Working
I'm trying to add a sound when the player collects all the item and I can't get it to work. Please help!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
public int jumpCount = 0;
public float maxSpeed = 5.0f;
public AudioClip winSound;
private Rigidbody rb;
private int count;
private AudioSource winSource;
private void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
jump = new Vector3(0.0f, 1.0f, 0.0f);
winSource = GetComponent<AudioSource>();
}
void OnCollisionStay() { isGrounded = true; jumpCount = 0; }
public void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && jumpCount == 0)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
jumpCount = jumpCount + 1;
}
if ( count >= 10)
{
winSource.clip = winSound;
winSource.Play();
}
}
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 10)
{
winText.text = "You Win!!!";
}
}
} ,I'm trying to add a sound when count >= 10 but it doesn't work please help!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
public int jumpCount = 0;
public float maxSpeed = 5.0f;
public AudioClip winSound;
private Rigidbody rb;
private int count;
private AudioSource winSource;
private void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
jump = new Vector3(0.0f, 1.0f, 0.0f);
winSource = GetComponent<AudioSource>();
}
void OnCollisionStay() { isGrounded = true; jumpCount = 0; }
public void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && jumpCount == 0)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
jumpCount = jumpCount + 1;
}
if ( count >= 10)
{
winSource.clip = winSound;
winSource.Play();
}
}
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 10)
{
winText.text = "You Win!!!";
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i check/wait until the gameobject will end the rotation ? 1 Answer
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer
Integer value randomly multiples itself by 3 or 2 for seemingly no reason. 2 Answers