Unity Tutorial : How to save data when moving between two scenes ( Using Player Prefs )

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 71

  • @catodevs
    @catodevs  4 ปีที่แล้ว +12

    Howsite Guys,
    Just finished with the second part of this video on playing music seamlessly across multiple scenes!
    If you want to check it out:
    th-cam.com/video/Xtfe5S9n4SI/w-d-xo.html

  • @DauDaGamer
    @DauDaGamer 9 หลายเดือนก่อน +1

    Dude you are helping us by making videos on topics that we all beginner wants to know, thank you

  • @uxe_yo9657
    @uxe_yo9657 4 ปีที่แล้ว +8

    Amazing videos! Those videos really helped me out with the new game that I hope to release soon on Steam! Looking forward to the next tutorial.

    • @catodevs
      @catodevs  4 ปีที่แล้ว +1

      No problem Bru🤟 hope your game takes off on steam 🚀

  • @tsunamidev
    @tsunamidev 2 ปีที่แล้ว +2

    THANK YOU SO MUCH
    its been painful to learn how to do this, you just solved one of my biggest issues

  • @svedalawoodcrafts
    @svedalawoodcrafts ปีที่แล้ว +2

    I think having it in a function that gets{} the values before switching scene is the correct way to do it rather than having them all in Update(). For example on slider you don't need that update that often, just takes up unnecessary memory and your only moving over the values on switching scene's?

  • @SamSamTutorialMan
    @SamSamTutorialMan ปีที่แล้ว +1

    Exactly what I needed. Thanks man!

    • @catodevs
      @catodevs  ปีที่แล้ว

      No problem!

  • @superdahoho
    @superdahoho 2 ปีที่แล้ว

    3:30
    do you have to declare the volume variable some where first?

  • @explodingmonkey44
    @explodingmonkey44 5 หลายเดือนก่อน

    I'm trying to do this for a toggle but I haven't been able to figure out what I'm doing wrong all day.
    It just keeps outputting true if it's toggled on or not.

  • @Xofol
    @Xofol 11 หลายเดือนก่อน

    You can also make real variables that saves between scenes by using static before public in your variable, but if you restart the game it resets tho.
    EX:
    static public testVariable;

  • @luckydenzel
    @luckydenzel ปีที่แล้ว

    When i switch from my main menu to level 1 the volume in level 1 switches the audio to 0,5 and on the main menu o selected 0,2

  • @qwertypaszzz5445
    @qwertypaszzz5445 4 ปีที่แล้ว +5

    Underrated video, seems like minor thing but its very important. Im sure that this video will help every viewer here and i hope u make another helpful video.

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Thanks for the great feedback mate!

    • @valorantduke65
      @valorantduke65 3 ปีที่แล้ว

      @@catodevs i hope you can create another vid like this but for HealthBar Slider. thanks for this helpful vid mate!

  • @evzyl8409
    @evzyl8409 4 ปีที่แล้ว +1

    it works thanks :) i hope u drop the next tutorial soon!

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Working on it! Will be ready soon 🤟

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Howsite bud,
      Just published the new video on how to play music seamlessly across multiple scenes ( part 3 of these videos ).
      Hope it is helpful :)
      th-cam.com/video/Xtfe5S9n4SI/w-d-xo.html

  • @RumbleKnightYT
    @RumbleKnightYT 4 ปีที่แล้ว +3

    Yess thank you bro! This really helped a lot

  • @leonardocdepaula
    @leonardocdepaula 3 ปีที่แล้ว +1

    Thanks Bro! You help me a lot!

  • @jirkasmajlik442
    @jirkasmajlik442 3 ปีที่แล้ว +1

    how can i use it for toggle please ?

  • @1HCs
    @1HCs 4 ปีที่แล้ว +1

    I'm getting a compiler error. - Heres my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class MainthemeScript : MonoBehaviour
    {
    // Start is called before the first frame update
    public AudioSource AudioSource;
    public Slider volumeSlider;

    public float musicVolume = 1f;
    void start()
    {
    AudioSource.volume = musicVolume;
    musicVolume = PlayerPrefs.Getfloat("colume");
    AudioSource.volume = musicVolume;
    volumeSlider.value = musicVolume;
    }
    // Update is called once per frame
    void Update()
    {
    AudioSource.volume = musicVolume;
    PlayerPrefs.Setfloat("volume", musicVolume)
    }
    public void updateVolume (float volume)
    {
    musicVolume = volume;
    }
    }

    • @catodevs
      @catodevs  4 ปีที่แล้ว +1

      Howsite Bud,
      The line:
      "PlayerPrefs.SetFloat("volume", MusicVolume)"
      is missing the ';' at the end... so should be:
      void Update()
      {
      AudioSource.volume = MusicVolume;
      PlayerPrefs.SetFloat("volume", MusicVolume);
      }
      Hope that helps!

  • @vhworks3428
    @vhworks3428 4 ปีที่แล้ว

    Works just fine. Thanks a bunch mate!

  • @emirhanpakyardm9142
    @emirhanpakyardm9142 3 ปีที่แล้ว +1

    Let's say i have a slider on scene1, and I don't have it on scene2. I just need the slider value to use it on scene2. What would i do?

    • @philippgyarmati6638
      @philippgyarmati6638 3 ปีที่แล้ว

      Dont mind if I ask, have you found the solution to that? Having the same problem right now

    • @philippgyarmati6638
      @philippgyarmati6638 3 ปีที่แล้ว

      Nvm, just found out how :D

    • @emirhanpakyardm9142
      @emirhanpakyardm9142 3 ปีที่แล้ว

      @@philippgyarmati6638 no, unfortunately

    • @emirhanpakyardm9142
      @emirhanpakyardm9142 3 ปีที่แล้ว

      @@philippgyarmati6638 oh tell me xD

    • @philippgyarmati6638
      @philippgyarmati6638 3 ปีที่แล้ว

      @@emirhanpakyardm9142 make a duplicate of the slider for every scene but make the sliders invisible where u dont want them, then pretend as if it is the original slider (put the audio source in the music player etc)

  • @combatmedic007
    @combatmedic007 3 ปีที่แล้ว

    YEEEEs finally i found the right thing ! Thanks

  • @sweetm4723
    @sweetm4723 3 ปีที่แล้ว +1

    thank you so much bro)👍

  • @WebBestMaster
    @WebBestMaster 2 ปีที่แล้ว +1

    thanks)) really useful

  • @seanmcculley1160
    @seanmcculley1160 4 ปีที่แล้ว

    Thanks, waiting on the 2nd part.

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Coming soon 🤟 will let you know

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Hey Man,
      Just finished with the 2nd part!
      Hope it is helpful :)
      th-cam.com/video/Xtfe5S9n4SI/w-d-xo.html

  • @micheleguardabasso8162
    @micheleguardabasso8162 2 ปีที่แล้ว

    Hi can you please in a project that i have to found?

  • @quantummobilegames
    @quantummobilegames 4 ปีที่แล้ว

    Works great ,thank you!

  • @mirzaemec8514
    @mirzaemec8514 3 ปีที่แล้ว

    works fine thanks a lot dude

  • @clarkmeyer7211
    @clarkmeyer7211 3 ปีที่แล้ว

    do you have a discord channel?

  • @dark_terion4459
    @dark_terion4459 3 ปีที่แล้ว

    at first it didn't work for me but I changed the float to a static float and it worked (I'm using this for FOV)

    • @evad3games320
      @evad3games320 2 ปีที่แล้ว

      Im like 10 months away from the post date if this comment, but fo you mind sharing how you created the FOV slider?

  • @BruuhGaming
    @BruuhGaming 2 ปีที่แล้ว

    Finaly, thanks mate

  • @raghavsrivastava10
    @raghavsrivastava10 2 ปีที่แล้ว

    Thank you very much!

  • @quantummobilegames
    @quantummobilegames 4 ปีที่แล้ว

    It works perfectly when I test it in unity. but when I export my project for android it doesn't save volume slider ,do you have any idea what is going wrong?

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Not sure mate, have sliders ever worked on your phone ? If they have try compare what's different between the two

  • @ploktor
    @ploktor 3 ปีที่แล้ว +1

    thanks, really usefull

  • @nrlafiefy8454
    @nrlafiefy8454 4 ปีที่แล้ว

    my audio source didn't appear . why?? this is my script ...
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class MusicPlay : MonoBehaviour
    {
    private AudioSource AudioSource;
    public Slider volumeSlide;
    private float MusicVolume = 1f;
    private void Start()
    {
    AudioSource.Play();
    MusicVolume = PlayerPrefs.GetFloat("volume");
    AudioSource.volume = MusicVolume;
    volumeSlide.value = MusicVolume;
    }
    // Update is called once per frame
    private void Update()
    {
    AudioSource.volume = MusicVolume;
    PlayerPrefs.SetFloat("volume", MusicVolume);
    }
    public void Volumeupdater(float volume)
    {
    MusicVolume = volume;
    }
    public void MusicReset()
    {
    PlayerPrefs.DeleteKey("volume");
    AudioSource.volume = 1;
    volumeSlide.value = 1;
    }
    }

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      You have 'private AudioSource AudioSource; '
      This means you won't be able to add an audio source on the inspector.
      so change it to:
      public AudioSource AudioSource;
      This should work.
      Let me know if that solves your problem :)

  • @TubebotGaming
    @TubebotGaming 3 ปีที่แล้ว

    Making this for Player Stats . Plz help me .

  • @clarkmeyer7211
    @clarkmeyer7211 3 ปีที่แล้ว +1

    SA representing :)

  • @pillemarisken
    @pillemarisken 3 ปีที่แล้ว

    Thanks for the tutorial! I'm getting this error though: "error CS0120: An object reference is required for the non-static field, method, or property 'AudioSource.Play()'"

    • @jeffreywei2749
      @jeffreywei2749 3 ปีที่แล้ว

      Check if you’ve dragged the audio source onto the script component, you have to reference the public component used in the script

  • @bangagus1703
    @bangagus1703 4 ปีที่แล้ว

    How to make that for mouse sensitivity

    • @catodevs
      @catodevs  4 ปีที่แล้ว

      Hi bud, sorry for the late response.
      Will have to make another video for that tutorial.

  • @emmanuelcajucom9506
    @emmanuelcajucom9506 2 ปีที่แล้ว

    error in value bro

  • @mamut_chino2419
    @mamut_chino2419 2 ปีที่แล้ว +1

    thanks

  • @kasunlee7489
    @kasunlee7489 2 ปีที่แล้ว

    Thanks!

  • @carlhaeggman2378
    @carlhaeggman2378 3 ปีที่แล้ว

    No more videos? ):

    • @catodevs
      @catodevs  3 ปีที่แล้ว +1

      Coming soon. Just in the process of immigrating and stuff haha when I'm settled I'll be making more videos 🤟🏼

    • @carlhaeggman2378
      @carlhaeggman2378 3 ปีที่แล้ว +1

      @@catodevs Nice!

  • @Christopher-krd
    @Christopher-krd 2 ปีที่แล้ว

    Thanks
    :D

  • @re-pro6037
    @re-pro6037 2 ปีที่แล้ว

    The volume slider option wont appear under my MusicControl thingy. Heres my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class MusicPlay : MonoBehaviour
    {
    // Start is called before the first frame update
    public AudioSource AudioSource;
    public Slider volumeSlider;
    public float musicVolume = 1f;
    void start()
    {
    AudioSource.volume = musicVolume;
    musicVolume = PlayerPrefs.Getfloat("colume");
    AudioSource.volume = musicVolume;
    volumeSlider.value = musicVolume;
    }
    // Update is called once per frame
    void Update()
    {
    AudioSource.volume = musicVolume;
    PlayerPrefs.Setfloat("volume", musicVolume);
    }
    public void updateVolume(float volume)
    {
    musicVolume = volume;
    }
    }

  • @shadowsethio2174
    @shadowsethio2174 4 ปีที่แล้ว +1

    Thanks!!!!!

  • @zakia-bin-salah
    @zakia-bin-salah 11 หลายเดือนก่อน +1

    But india failed to maintain UNITY..

  • @emmanuelcajucom9506
    @emmanuelcajucom9506 2 ปีที่แล้ว +4

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Slider : MonoBehaviour
    {
    private AudioSource AudioSource;
    public Slider volumeSlide;
    private float MusicVolume = 1f;
    private void Start()
    {
    AudioSource.Play();
    MusicVolume = PlayerPrefs.GetFloat("volume");
    AudioSource.volume = MusicVolume;
    volumeSlide.value = MusicVolume;
    }
    // Update is called once per frame
    private void Update()
    {
    AudioSource.volume = MusicVolume;
    PlayerPrefs.SetFloat("volume", MusicVolume);
    }
    public void Volumeupdater(float volume)
    {
    MusicVolume = volume;
    }
    public void MusicReset()
    {
    PlayerPrefs.DeleteKey("volume");
    AudioSource.volume = 1;
    volumeSlide.value = 1;
    }
    }

  • @NOTAHOWTOCHANNEL
    @NOTAHOWTOCHANNEL ปีที่แล้ว

    Thank you soooo much!