3
\$\begingroup\$

I attached music file to my first scene and thanks to following javascipt code, sound continues without stopping in other scenes.

public static var object : SingletonMusic = null;

function Awake()
{

    if( object == null )
    {
        object = this;
        DontDestroyOnLoad(this);
    }
    else if( this != object )
    {
        Destroy( gameObject );
    }
}

My problem is that I want to turn off the sound from the button in the another scene (setting scene) so that I added a new button to my setting scene and attached the following javascript code to the button that I have created.

Javascript code:

var objects: AudioSource = SingletonMusic.object.GetComponent(AudioSource); 
if( objects.isPlaying ) 
objects.Pause(); 
else 
objects.Play();

However, it gives following errors:

If I start the game from Setting scene; I get this error:

enter image description here

If I start the game from first scene and then, go to Setting scene; I get this error:

enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It says that you are calling a gmeobject in a field initializator or constructor. Instead try adding initialization to Start or Awake method.

This is probably what you do:

public go = gameObject;

Try doing this instead:

public go;

void Start()
{
    go = gameObject;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .