Search Unity

Resolved Please help how to add that the sprite saves when change scene

Discussion in 'Scripting' started by xavierke, Jul 26, 2020.

  1. xavierke

    xavierke

    Joined:
    Jul 26, 2020
    Posts:
    12
    So i created a script for my audio. But when i click on my mute/unmute button it goes to sprite off. And when i click it for the second time its set to on whats supossed to happen. (by on or off the sprite chanches) But when i go to a diffrent scene the sprite returns to on while the audio is off <sprite = musicOff.png or musicOn.png file>
    So i need a way that the sprite saves to off and gets loaded back to off when i revisit the MainMenu scene

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Manager : MonoBehaviour {
    7.    
    8.     public GameObject soundControlButton;
    9.     public Sprite audioOffSprite;
    10.     public Sprite audioOnSprite;
    11.    
    12.     //use this for initialization
    13.     void start() {
    14.         if (AudioListener.pause == true) {
    15.             soundControlButton.GetComponent<Image> ().sprite = audioOffSprite;
    16.         } else {
    17.             soundControlButton.GetComponent<Image> ().sprite = audioOnSprite;
    18.         }
    19.     }
    20.    
    21.     //Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25.    
    26.     public void SoundControl(){
    27.         if (AudioListener.pause == true){
    28.             AudioListener.pause = false;
    29.             soundControlButton.GetComponent<Image> ().sprite = audioOnSprite;
    30.         } else {
    31.             AudioListener.pause = true;
    32.             soundControlButton.GetComponent<Image> ().sprite = audioOffSprite;
    33.         }  
    34.     }
    35. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Capitalization and spelling and punctuation are CRITICAL in programming.

    Line 13 above, the correct spelling for the Unity MonoBehavior "Start()" function requires an uppercase S.
     
    xavierke likes this.
  3. xavierke

    xavierke

    Joined:
    Jul 26, 2020
    Posts:
    12
    Tnx u made my day :)
     
    Kurt-Dekker likes this.