Advertisement

AudioSource and Finding Object to Destroy

Started by December 14, 2016 12:10 AM
2 comments, last by Zesi 7 years, 11 months ago

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.

Hey there

first are you sure you have set the Audio File correctly to not be null? Unity does save Prefabs different than it does with Scenes and settings made on scene objects. Check for it

Second, you are seeking any spawner at the start so why call destroy with "Spawner" (except that there is a ';' missing) and not change spawner to be an array and seek for ObjectsOfType<Spawner>(); later use them in a loop to call GameObject.Destroy(spawner) ?

Advertisement
Hi Shaarigan. Thank you.

1) How do I set the audio file to not be null? I have alreadt attach the audio file to the audiosource via inspector. How do I script AudioSource to select the attached audio file instead of null?

2) Okay. Let me try the array method.

Thank you very much!

(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?

This topic is closed to new replies.

Advertisement