- Home /
Question by
tweakingtechnologies · May 24, 2019 at 07:14 AM ·
scripting problemsound
Getting errors after destroying objects and playing sound?
I have got power ups popping on my screen. Now there are three Sound files that need to be played.
When power up is spawned into the scene
When User actually taps on the power up
When user fails to tap on power up and then power up destroys itself after a fixed interval of time.
My code works fine as in I am getting the desired behavior. But then I get these errors whenever i change the scene.
All my sound files are stored in a
global/persistant SoundManager Class
. This script is attached to the "PowerUp Prefab".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaySound : MonoBehaviour
{
public bool touched = false;
GameManager _gm;
SoundManager _sm;
// Start is called before the first frame update
void Start()
{
_gm = GameObject.Find("GameManager").GetComponent<GameManager>();
_sm = GameObject.Find("SoundManager").GetComponent<SoundManager>();
if (_gm.GetSoundFlag())
AudioSource.PlayClipAtPoint(_sm.PowerUpSpawn, Camera.main.transform.position);
}
private void OnDestroy()
{
if(_gm.GetSoundFlag() && touched)
{
AudioSource.PlayClipAtPoint(_sm.PowerUpHit, Camera.main.transform.position);
}
else if(_gm.GetSoundFlag() && !touched)
{
AudioSource.PlayClipAtPoint(_sm.PowerUpVanish, Camera.main.transform.position);
}
}
// Update is called once per frame
}
The errors that i get are
error.png
(10.1 kB)
Comment