Hi all,
My problem is the following:
I am making a simple unity game with C#, and i need that game over music starts when the ball (that is the player) was placed under -5 in y axis. I have achieved to stop background music, but i am not able to play the game over music. It sounds only a second decimal and stop. I belive that it is because i am doing it in update method. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Jugador : MonoBehaviour
{
public float velocity;
private Rigidbody fisic;
private int points;
private Transform position;
public Text gameOver;
private GameObject camera;
// Start is called before the first frame update
void Start(){
fisic = GetComponent();
position = GetComponent();
camera = GameObject.FindGameObjectWithTag(“camera”);
}
// Update is called once per frame
void Update(){
float movimientoHorizontal = Input.GetAxis(“Horizontal”); //horizontal movement
float movimientoVertical = Input.GetAxis(“Vertical”); //vertical movement
Vector3 movimientos = new Vector3(movimientoHorizontal, 0, movimientoVertical);
fisic.AddForce(movimientos * velocity);
//we control when the ball falls into empty space
if (position.localPosition.y < -5) {
gameOver.text = “GAME OVER”;
managerMusic();
}
}
void managerMusic() {
camera.GetComponent().Stop(); // it works
GetComponent().Play(); // it doesnt work !!!
}
void OnGUI(){
GUI.Box(new Rect(0, 0, 80, 35), "Score: " + points);
}
private void OnTriggerEnter(Collider collision){
if (collision.CompareTag(“coin”)) {
points++;
}
}
}
