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. Dismiss Notice

Exist one method for that one PlayerPrefs work in only one GameObject

Discussion in 'Scripting' started by the_line_6969, Jul 5, 2018.

  1. the_line_6969

    the_line_6969

    Joined:
    Feb 3, 2018
    Posts:
    14
    My problem is thati have some gameobjects whit the same script, and in this script, exist one playerpref, but if only one playerpref was actived, all gameobjects was actived.

    For that, my question is if exist one method that make that the playerprefs work in just one game object, or if exits another way to do
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    PlayerPrefs, overall, is shared by the app/game. Each key is unique.

    Maybe you could expand your question a bit with an example?
     
  3. the_line_6969

    the_line_6969

    Joined:
    Feb 3, 2018
    Posts:
    14
    Sorry, I can wrotre before.

    First, said that I explain bad, because I refer, as you said, to the keys.

    Second, I dont use one player prefs that afect one float, string or int, i use one boolean, that can be actived follow this link

    http://wiki.unity3d.com/index.php/BoolPrefs

    Said that, this is the code that i use in one model of minimap created for myself, that if is actived one image appear in one canvas, this image represent the floors

    This is the script that i put in one box collider that detect when the player enter on one floor

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class Estado2 : MonoBehaviour {
    7.  
    8.     public bool Trigger2 = false;
    9.  
    10.     public Image img;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         img.enabled = false;
    15.         //PlayerPrefsX.SetBool("Trigger2", false);
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (Trigger2)
    21.         {
    22.             img.enabled = true;
    23.             //PlayerPrefsX.SetBool("Trigger2", true);
    24.         }
    25.         if (PlayerPrefsX.GetBool("Trigger2", true))
    26.         {
    27.             Trigger2 = true;
    28.         }
    29.     }
    30.  
    31.     void OnTriggerEnter2D(Collider2D collision)
    32.     {
    33.         if (collision.gameObject.tag == "Player")
    34.         {
    35.             Trigger2 = true;
    36.         }
    37.     }
    38.     void OnTriggerExit2D (Collider2D collision)
    39.     {
    40.         if (collision.gameObject.tag == "Player")
    41.         {
    42.             Trigger2 = false;
    43.         }
    44.     }
    45. }
    And this is the code that i put in one gameobject that is the son of the canvas

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class Estado22 : MonoBehaviour {
    7.  
    8.     public Image img;
    9.  
    10.     public Estado2 estado2;
    11.  
    12.     void Start () {
    13.         PlayerPrefsX.SetBool("Trigger2", false);
    14.     }
    15.  
    16.     void Update () {
    17.         if (PlayerPrefsX.GetBool("Trigger2", true))
    18.         {
    19.             estado2.Trigger2 = true;
    20.         }
    21.     }
    22. }
    The problem is that in my desing, exist some floors, and if i active one, all is actived, and i dont know how i can make that the playerprefs works only in one script, what you recomend me, use the json?, i dont know so much.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If I understood you correctly, you want a unique key for each floor so you can distinguish between them. Whether you use player prefs or Json is up to you. The idea is the same.. :)
     
  5. the_line_6969

    the_line_6969

    Joined:
    Feb 3, 2018
    Posts:
    14
    The problem when i said that is some floors i am talking for maybe 200 floors, and i dont think that this is one optimizated form, yout think that can affect or be bad? I dont know nothing of json,and in the moment i dont want to know