Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback I Need Help In Using PlayerPrffs

Discussion in 'Scripting' started by RyanGamingKob, Nov 3, 2019.

  1. RyanGamingKob

    RyanGamingKob

    Joined:
    Oct 27, 2019
    Posts:
    18
    So I Made A Working Level Selector Using A Brackeys Video
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelSelector : MonoBehaviour
    7. {
    8.     public LevelFaderScript fader;
    9.     public Button[] levelsButtons;
    10.  
    11.     void Start()
    12.     {
    13.         int levelReached = PlayerPrefs.GetInt("levelReached", 1);
    14.  
    15.         for (int i = 0; i < levelsButtons.Length; i++)
    16.         {
    17.             if (i + 1 > levelReached)
    18.             {
    19.                 levelsButtons[i].interactable = false;
    20.             }
    21.            
    22.         }      
    23.  
    24.     }
    25.  
    26.     public void SelectLevel(int levelIndex)
    27.     {
    28.         fader.FadeToLevel(levelIndex);
    29.     }
    30.  
    31. }
    32.  
    33.  
    And I Want In The Game Manager, In The PlayerPrefs, Use This Function: PlayerPrefs.SetInt("levelReached", levelReached + 1);
    But I Cannot Use The LevelReached variable since it is in another script.
    Will it work if I Use static variable, or no?
    I just want to increase the levelReached when I Win A Level and pass the other
    Somebody help me!
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Make the variable/field public or use properties (get and set accessors). Then you need to just reference the object somehow in your code, like making a public field where you drag your
    another script. Now you can access that data in your current code.

    For example,

    Public field:
    Code (CSharp):
    1. public int levelReached;
    Get/Set accessors:
    Code (CSharp):
    1. // Your private variable
    2. private int _levelReached;
    3.  
    4. // public access to it
    5. public int LevelReached
    6. {
    7.     get { return _levelReached; }
    8.     set { _levelReached = value; }
    9. }
    How to reference another location:
    you need to fill this field either in code or in Inspector by dragging and object in that contains the script GameManager:
    Code (CSharp):
    1. GameManager gameManager;
    When you need to access that level value, you can:
    Code (CSharp):
    1. // Print it out to Debug log
    2. Debug.Log("LevelReached: " + gameManager.LevelReached);
    3.  
    4. // Increment the level counter by one
    5. gameManager.LevelReached++;
     
    Last edited: Nov 3, 2019
    RyanGamingKob likes this.
  3. RyanGamingKob

    RyanGamingKob

    Joined:
    Oct 27, 2019
    Posts:
    18
    The problem is that The LevelSelector Script And The GameManager Are In DifferentScene
    The Level Selector Is In The LevelSelector Scene And The Game manager is in the level01
    so i couldn't use a public variable, maybe a static? but i want to save the progress
    what should i do then
     
  4. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    you already answered your own question.
    make it static.
    I mean get rip of MonoBehaviour

    Code (CSharp):
    1. public static class GameDB
    2.     {
    3.         public static int levelReached
    4.         {
    5.             get => PlayerPrefs.GetInt("levelReached", 0);
    6.             set => PlayerPrefs.SetInt("levelReached", value);
    7.         }
    8.     }

    however put things in static as global, was not a good practice.
    use it wisely.

    OR~

    if you have any requirement depend on MonoBehaviour.
    try to implement the singleton design pattern.
    https://wiki.unity3d.com/index.php/Singleton
    and make it `DontDestroyOnLoad`
     
    Last edited: Nov 4, 2019