Variable with same value in every scene

Hello guys im trying to make a music button where you can turn off/on music, i created it succesfuly, but when I turn off music in one scene, in other is turned On again as it defaulty is. And so when i turn off music in one scene i want it to be turned off in other too. I was thinking about creating a bool which checks if the button is on or off, it will be set true by default so music will play on start the game, but once bool is set to false it wont be played in other scene until i click music button again to turn it on. The check bool should be somewhere on beggining the scene. Does anybody know the best way for such variables which doesnt change in every initialize(in every scene) until I make a change through script or so?

Hi @smokys,

I tested this and you can see my results here. (The scene “1” is the one with music on. Scene “Untitled” is the one with the music off)

Hope you get the answer you were looking for!

Hello, i cannot hear the music in scene 1, nor the button for turning music on/of as well as changing scene so i can compare.

This is just a prototype. I don’t have any music unless you can link a music beat I can use?

Well, audiojunge offers free music with their mark in it. but my question was something else, i want variable which have the same value in every scene. Example: i create variable score = 0; during a play in one scene i get score 25, and i want that amount to stay in scene 2

1 Like

If can, send a link to project or is it private?

Why would you need that for? And yes its private.

It’s easier for me to look at the project but it’s fine :smiley:

Well i dont need to fix a problem, i just need an explanation, i think its easy but i cant find examples for that

1 Like

Oh I got confused. Sorry!

Yeah no problem. Just need to know how to set up such variable

Edit: For variables I suggest to actually make a script?

Please, i really appreciate your will to help, but these things you saying are actualy useless for me.

That is the thing im saying from beggining

We flooded this topic with actual useless things

1 Like

Yeah, sorry I try to help everyone and I normally only read the stuff that is the problem not the whole thing. I just read it so that’s why I edited it :sweat:

Well read what people say, we are just spamming this topic with no reason, so please if you have no answer for actual question i just mentioned then dont reply, let people help with actual problem, now they have to read all this.

1 Like

Hi @smokys,

You can easily create global variables by using the window object (though I’d avoid that in bigger/production projects):

window.musicState = true;

Another alternative to better organize this is to use the script object:

var MyScript = pc.createScript('myScript');

MyScript.musicState = true;

MyScript.prototype.initialize = function () {
   // changing the value here, will update the property for any script instance
   MyScript.musicState = false;

Thank you @Leonidas, about second method - to access the script do i need to only use object.script.MyScript , because that seemed to cause more problems lately for me(similiar to not unsubscribe to global event).

EDIT: well i actually can use event to change value, rigth? :slight_smile:

But what if i want to compare value in another script? Anyway it helped, but now i need to figure out how to set proper button texture, as defaultly it is set to show “PlayingMusic” image but when i turn off the music and come in another scene, music is off finally, but is showing “PlayingMusic” image instead of “NoPlayingMusic” - i need to compare the value in script where i set button texture. Is there any other way of accessing the script besides object.script.MyScript.musicState ?

Maybe something like this?

var music = object.script.MyScript.musicState;

That is the one causing me some problems lately, had to use events rather and it solved problem.

1 Like

Okay i solved it through the events, as always :stuck_out_tongue: thank you for help guys, i really appreciate it.

1 Like