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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

change the game character to unlocked new character

Discussion in 'Scripting' started by ArshidaGames, Sep 1, 2019.

  1. ArshidaGames

    ArshidaGames

    Joined:
    Feb 9, 2019
    Posts:
    93
    how when i buy character 2 and i go to gameplay scene. i see the new unlocked prefab2?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class BuyScript : MonoBehaviour
    5. {
    6.     public int myChar;
    7.     private int price;
    8.     public Text text;
    9.     public Text priceText;
    10.     void Start()
    11.     {
    12.         price = GetComponent<BuyScript2>().price;
    13.     }
    14.     void Update()
    15.     {
    16.         if (myChar == 1)
    17.         {
    18.             if (GlobalValues.character == 1)
    19.             {
    20.                 text.text = "selected";
    21.                 priceText.text = "";
    22.             }
    23.             else
    24.             {
    25.                 text.text = "select";
    26.             }
    27.         }
    28.         if (myChar == 2)
    29.         {
    30.             if (GlobalValues.character2Unlocked)
    31.             {
    32.                 if (GlobalValues.character == 2)
    33.                 {
    34.                     text.text = "selected";
    35.                     priceText.text = "";
    36.                 }
    37.                 else
    38.                 {
    39.                     text.text = "select";
    40.                     priceText.text = "";
    41.                 }
    42.             }
    43.             else
    44.             {
    45.                 text.text = "$" + price;
    46.             }
    47.         }
    48.         if (myChar == 3)
    49.         {
    50.             if (GlobalValues.character3Unlocked)
    51.             {
    52.                 if (GlobalValues.character == 3)
    53.                 {
    54.                     text.text = "selected";
    55.                     priceText.text = "";
    56.                 }
    57.                 else
    58.                 {
    59.                     text.text = "select";
    60.                     priceText.text = "";
    61.                 }
    62.             }
    63.             else
    64.             {
    65.                 text.text = "$" + price;
    66.             }
    67.         }
    68.         if (myChar == 4)
    69.         {
    70.             if (GlobalValues.character4Unlocked)
    71.             {
    72.                 if (GlobalValues.character == 4)
    73.                 {
    74.                     text.text = "selected";
    75.                     priceText.text = "";
    76.                 }
    77.                 else
    78.                 {
    79.                     text.text = "select";
    80.                     priceText.text = "";
    81.                 }
    82.             }
    83.             else
    84.             {
    85.                 text.text = "$" + price;
    86.             }
    87.         }
    88.         if (myChar == 5)
    89.         {
    90.             if (GlobalValues.character5Unlocked)
    91.             {
    92.                 if (GlobalValues.character == 5)
    93.                 {
    94.                     text.text = "selected";
    95.                     priceText.text = "";
    96.                 }
    97.                 else
    98.                 {
    99.                     text.text = "select";
    100.                     priceText.text = "";
    101.                 }
    102.             }
    103.             else
    104.             {
    105.                 text.text = "$" + price;
    106.             }
    107.         }
    108.     }
    109. }
     
    Last edited: Sep 2, 2019
  2. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    Using global variables come with a lot of overhead. The system you are using seems overly complicated and I would simplify your system. You could try something like this...

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class PlayerManager: MonoBehaviour
    5. {
    6.     // stores the locks for Player
    7.     private string PlayerLocks { get; set; }
    8.     private string PlayerPrice { get; set; }
    9.     public static PlayerManager Instance { get; set; }
    10.  
    11.     // reset lock keys
    12.     private void DefaultLocks()
    13.     {
    14.         // 1 = locked player
    15.         // 0 = unlocked player
    16.         // first one should be unlocked.
    17.         PlayerLocks = "01111";               //EDIT ME
    18.         PlayerPrice = "0,100,200,300,400";   //EDIT ME
    19.         SavePlayerKeys();
    20.     }
    21.  
    22.     public bool LockedPlayer(int playerId)
    23.     {
    24.         // if 0 value sent, return true
    25.         if (playerId.Equals(0)) return true;
    26.  
    27.         int locked = Convert.ToInt32(PlayerLocks[playerId - 1].ToString());
    28.  
    29.         return Convert.ToBoolean(locked);
    30.     }
    31.  
    32.     public float PricePlayer(int playerId)
    33.     {
    34.         float playerPrice = 0;
    35.  
    36.         // if 0 value sent, return true
    37.         if (playerId.Equals(0)) return playerPrice;
    38.  
    39.         string[] splits = PlayerPrice.Split(',');
    40.    
    41.         float.TryParse(splits[playerId - 1], out playerPrice);
    42.  
    43.         return playerPrice;
    44.     }
    45.  
    46.     public void UnLockPlayer(int playerId)
    47.     {
    48.         // if 0 value sent, return
    49.         if (playerId.Equals(0)) return;
    50.  
    51.         // stores new locks.
    52.         string tempKeys = "";
    53.  
    54.         for (int i = 0; i < PlayerLocks.Length; i++)
    55.         {
    56.             char c = PlayerLocks[i];
    57.             if (i == playerId-1) c = '0';
    58.  
    59.             tempKeys += c;
    60.         }
    61.  
    62.         PlayerLocks = tempKeys;
    63.         SavePlayerKeys();
    64.     }
    65.  
    66.     void Awake()
    67.     {
    68.         Instance = this;
    69.         DontDestroyOnLoad(gameObject);
    70.  
    71.         if (!PlayerPrefs.HasKey("PlayerKeys"))
    72.             DefaultLocks();
    73.         else
    74.             LoadPlayerKeys();
    75.     }
    76.  
    77.     // save lock keys
    78.     private void SavePlayerKeys()
    79.     {
    80.         PlayerPrefs.SetString("PlayerKeys", PlayerLocks);
    81.         PlayerPrefs.SetString("PlayerPrice", PlayerPrice);
    82.         PlayerPrefs.Save();
    83.     }
    84.  
    85.     // load lock keys
    86.     private void LoadPlayerKeys()
    87.     {
    88.         PlayerLocks = PlayerPrefs.GetString("PlayerKeys");
    89.         PlayerPrice = PlayerPrefs.GetString("PlayerPrice");
    90.     }
    91. }
     
    Last edited: Sep 2, 2019
    ArshidaGames likes this.
  3. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    Here is a sample manager for the player manager.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GameManager: MonoBehaviour
    4. {
    5.     public PlayerManager _playerManager;
    6.  
    7.     void Start()
    8.     {
    9.         _playerManager = PlayerManager.Instance;
    10.     }
    11. }
     
    ArshidaGames likes this.
  4. ArshidaGames

    ArshidaGames

    Joined:
    Feb 9, 2019
    Posts:
    93
    in my Buyscript :

    by default character 1 is free and open to use. however at the moment if you start the game and go to the shop its ui text is showing select.
    i would like it shows selected because it is free.
    and then when user bought a new character , then character 1 ui text become select and new unlocked character ui text shows selected instead.
    can someone say how to do that?
    thanks for any help.
     
    Last edited: Sep 2, 2019