Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

The Console Shows an error of cannot find instance and it also doesn't continue song as scene change

BuriuddinBuriuddin Member
edited October 2020 in Programming

I have made my own game in unity3d and I wanted to have audio so I wrote a script using Brackeys Audio tutorial. I did as he showed but I got a problem. I have a Audio Manager in two scenes and I want my audio to be continued as I switch scenes. Also I want one audio to be played when Game has ended. I don't know how to fix a bug. It shows me that I don't have a instance. I think that there are some changes in the unity audio system as the video was about 2-3 years back. The codes are as follows :


The code named Audio Manager

`using UnityEngine;

using UnityEngine.Audio;

using System;


public class AudioManager : MonoBehaviour

{

   public Sound[] sounds;

   public static AudioManager instance;


   void Awake()

   {

       if (instance == null)

       {

           instance = this;

       }

       else

       {

           Destroy(this);

           return;

       }

       DontDestroyOnLoad(gameObject);



       foreach (Sound s in sounds)

       {

           s.source = gameObject.AddComponent<AudioSource>();

           s.source.clip = s.clip;


           s.source.volume = s.volume;

       }

   }


   public void Play (string player)

   {

       Sound s = Array.Find(sounds, sounds => sounds.nameOfSound == player);

       s.source.Play();

   }


   private void Start()

   {

       Play("MemoriesMainTheme");

   }

}`



Now this is the script of which is named Sound

`using UnityEngine;

using UnityEngine.Audio;


[System.Serializable]

public class Sound

{

   public string nameOfSound;

   public bool loop;


   public AudioClip clip;



   [Range(0f,2f)]

   public float volume;


   [HideInInspector]public AudioSource source;

}`


Please help me if anyone knows how to solve it. I am new to using Unity and this is my first game with alot of problems. Please Please help me

Sign In or Register to comment.