Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 17, 2021 at 08:25 PM by Dan5500 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dan5500 · Jan 16, 2021 at 04:14 PM · startboolrunonce

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Jan 16, 2021 at 04:40 PM

  1. Remove the callbacks you've set on the Toggles from the inspector

  2. Remove the Options component from the hierarchy

  3. Replace the code of the Options class by the one below

  4. Create a new script called OptionToggles and add it to the hierarchy (see script below)

  5. 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);
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dan5500 · Jan 16, 2021 at 10:41 PM 0
Share

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.

avatar image Hellium Dan5500 · Jan 16, 2021 at 10:44 PM 1
Share
  • Why do you pause / unpause the music in Update rather than in Start?

  • Is the value of Options correctly retrieved in your $$anonymous$$usic class? (Add a Debug.Log to find out)

avatar image Dan5500 Hellium · Jan 17, 2021 at 02:07 AM 0
Share

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)

Show more comments

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate an object and destroy it after given time 2 Answers

Cant find component after scene load but it exists ! HELP! 0 Answers

Game does not start in Xiaomi Redmi 4A 0 Answers

Game starts even on Menu screen! 0 Answers

Referenced Bool is not updating 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges