- Home /
Audio is fine in one scene but not in another scene
Hello i have a Player prefab who is available in all scenes it has 4 Audio sources when collecting with different gameobjects each sounds plays but in the next scene it gives an error that the variable is not assigned("The Variable RedCoin of the PlayerController script is not assigned") this is the code:
public Text counttext;
private int count;
private Rigidbody rb3d;
public int speed;
public AudioSource[] sounds;
public AudioSource coin;
public AudioSource Stanlee;
public AudioSource RedCoin;
public AudioSource BlueCoin;
//public AudioSource coin;
//public AudioSource Stanlee;
private void FixedUpdate()
{
float moveh = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveh, 0.0f, moveV);
rb3d.AddForce(movement * speed);
}
private void Start()
{
sounds = GetComponents<AudioSource>();
rb3d = GetComponent<Rigidbody>();
count = 0;
Setcount();
coin = sounds[0];
Stanlee = sounds[1];
RedCoin = sounds[2];
BlueCoin = sounds[3];
//coin = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Coin"))
{
other.gameObject.SetActive(false);
count = count + 5;
coin.Play(); //ERROR IS HERE
Setcount();
//Debug.Log("Hello Shit");
}
else if (other.gameObject.CompareTag("RedCoin"))
{
other.gameObject.SetActive(false);
count = count + 10;
RedCoin.Play(); //ERROR IS HERE
Setcount();
}
else if (other.gameObject.CompareTag("BlueCoin"))
{
other.gameObject.SetActive(false);
count = count + 50;
Setcount();
BlueCoin.Play(); //ERROR IS HERE
}
else if (other.gameObject.CompareTag("Mine"))
{
SceneManager.LoadScene("GameOver");
}
else if (other.gameObject.CompareTag("Stanlee"))
{
SceneManager.LoadScene("GameOver");
}
else if (other.gameObject.CompareTag("LC"))
{
SceneManager.LoadScene("Main2");
}
else if (other.gameObject.CompareTag("LC2"))
{
SceneManager.LoadScene("Main3");
}
else if (other.gameObject.CompareTag("Stanleez"))
{
Stanlee.Play(); //ERROR IS HERE
}
}
void Setcount()
{
counttext.text = "Score : " + count.ToString();
}
Answer by game4444 · May 08, 2018 at 07:15 AM
You are using Public AudioSource in your script. So in every Scene you have to assign your public AudioSource objects. It will give you this error if you won't assign
I cannot assign the Audio sources because i cant drag the audio files to those public slots any help?
Answer by alideveloper95 · May 08, 2018 at 04:10 PM
try to use AudioSource audio;
if(audio == null) debug.log("WHT?");
instead of play () function audio.PlayOneShot(name of audio);
Your answer
Follow this Question
Related Questions
Sound plays right at the beginning 2 Answers
Playing each channel of a midi track on separate audio sources? 0 Answers
Multiple Cars not working 1 Answer
Using OnAudioFilterRead with playOnAwake 0 Answers
Audio & SpaceShip problems 1 Answer