Sign in to follow this  

AudioSource and Finding Object to Destroy

This topic is 368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by Zesi

Share this post


Link to post
Share on other sites

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[i]) ?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

This topic is 368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this