Hi all. Facing some slight problem again.
1) I am trying to play an AudioSource component on the current UI object.
2) I have 5 Spawner Prefabs Object in the scene. (Spawner is the script name attached to the GameObject "Spawner")
private AudioSource victorySound;
private Spawner spawner;
void Start (){
victorySound = GetComponent<AudioSource>();
spawner = GameObject.FindObjectOfType<Spawner> ();
}
// In between there are other method calling for OnVictory
void OnVictory (){
victorySound.Play ();
Destroy ("Spawner")
Invoke ("NextLevel", 3f);
}
void NextLevel(){
SceneManager.LoadScene ("VictoryScreen");
}
It does load victory screen, meaning the invoke works.
However,
1) it doesn't play the clip that I have attached to the inspector and gave me the warning that "victorySound is not assigned and it's default value will be null"
2) it doesn't destroy all the spawner objects at all.
What am I doing wrong here?
thank you all!
Edit: (2) Solved! Thank you. I have change my code for spawner only to.
private Spawner[] spawnerArray;
void Start () {
// other lines of code.
spawnerArray = GameObject.FindObjectsOfType<Spawner> ();
}
void OnVictory (){
// other lines of code.
foreach (Spawner spawner in spawnerArray) {
Destroy (spawner.gameObject);
}
Now it destroy all spawner upon victory.
However, there is an error,
"The object of type 'Spawner' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object."
I have another script (Shooter) that is constantly accessing Spawner[]. What do I do to to that method once Spawner is all destroyed?
I am still unable to play Audio.