The question is answered, right answer was accepted
How to make a line of code run once in C#?
Alright, so I'm currently developing a 3D platformer game, and I am working on an options bug. This bug being that I can turn a bool to false, and then I go into a level, go back into the same menu Scene, and the music starts playing even though I turned it off before. I am using a toggle to turn on and off the bool.
Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Options : MonoBehaviour
{
public static bool isMusic;
public static bool isCoinSFX;
void Start()
{
isMusic = true;
isCoinSFX = true;
}
void Update()
{
}
public void MusicValueChange()
{
isMusic = !isMusic;
}
public void CoinSFXChange()
{
isCoinSFX = !isCoinSFX;
}
}
Yes, I know I have to remove the code in the Start, but then the Toggle will be inverted. (When it's toggled off, the music is on and vise versa) Please help if you see this and know the answer.
Answer by Hellium · Jan 16, 2021 at 04:40 PM
Remove the callbacks you've set on the Toggles from the inspector
Remove the
Options
component from the hierarchyReplace the code of the
Options
class by the one belowCreate a new script called
OptionToggles
and add it to the hierarchy (see script below)Drag & drop the toggles in the inspector of the
OptionToggles
component
public static class Options
{
public static bool isMusic;
public static bool isCoinSFX;
}
using UnityEngine;
using UnityEngine.UI;
public class OptionToggles : MonoBehaviour
{
public Toggle musicToggle; // Drag & drop in inspector
public Toggle coinsSFXToggle; // Drag & drop in inspector
private void Start()
{
musicToggle.isOn = Options.isMusic;
coinsSFXToggle.isOn = Options.isCoinSFX;
musicToggle.onValueChanged.addListener(value => Options.isMusic = value);
coinsSFXToggle.onValueChanged.addListener(value => Options.isCoinSFX = value);
}
}
Ok, so I did what you had suggested and I don't see anything changed. The music is still playing after I go back into the scene. What code do you need to see? The code for my music is the following:
// this is $$anonymous$$usic.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$usic : $$anonymous$$onoBehaviour
{
public AudioSource Background$$anonymous$$usic;
void Start()
{
Background$$anonymous$$usic.UnPause();
}
void Update()
{
if (Options.is$$anonymous$$usic)
{
Background$$anonymous$$usic.UnPause();
}
else
{
Background$$anonymous$$usic.Pause();
}
}
}
If you need to see any other code then ask. Thank you for helping.
Why do you pause / unpause the music in
Update
rather than inStart
?Is the value of
Options
correctly retrieved in your$$anonymous$$usic
class? (Add a Debug.Log to find out)
Yes, the value of Options
is correctly retrieved. I checked using Debug.Log(Options.is$$anonymous$$usic);
. Also, I pause and unpause it in Update
because when you toggle it then it wont be changed automatically.
WHAT IS SETTING is$$anonymous$$usic TO TRUE?!?!? This is the code that is in OptionsToggles.cs and Options.cs is the following:
Options.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Options
{
public static bool is$$anonymous$$usic;
public static bool isCoinSFX;
}
OptionToggles.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OptionToggles : $$anonymous$$onoBehaviour
{
public Toggle musicToggle;
public Toggle coinsSFXToggle;
private void Start()
{
musicToggle.isOn = Options.is$$anonymous$$usic;
coinsSFXToggle.isOn = Options.isCoinSFX;
musicToggle.onValueChanged.AddListener(value => Options.is$$anonymous$$usic = value);
coinsSFXToggle.onValueChanged.AddListener(value => Options.isCoinSFX = value);
}
public void Change$$anonymous$$usic()
{
Options.is$$anonymous$$usic = !Options.is$$anonymous$$usic;
}
public void ChangeSFX()
{
Options.isCoinSFX = !Options.isCoinSFX;
}
}
Those two void methods are used for the toggles to toggle on and off. (Unless the code your using is already doing that)