saving and loading a bool on different objects that share the same script

Updated on July 5, 2017 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on July 5, 2017

Hi
 
I have a Level selector with 5 level buttons (when clicked coins are subtracted and they go to different scenes)
 
I have them in the menu scene from the beginning because I have a zoom animation on the buttons rect scroll that I would like to keep.
https://www.youtube.com/watch?v=OHChC-K-HpI
 
Everything works except for the Bool unlocked
 
The problem is the buttons share the same script that has the Unlocked Bool, So when unlocked = true all the buttons are unlocked, instead of just the button pressed only.
 
How would I give each button a separate unlocked bool?
Would that be via a separate  Level/GameManager Script?
or via List or Bool Array?
 
Thank you.
 

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System;
 using System.Security.Cryptography.X509Certificates;
 using UnityEngine.SceneManagement;
  
 public class LevelButtonNew : MonoBehaviour
 {
  
     public Button myButton;
     public Text LevelText;
     public bool unlocked;
     public int characterValue;
     public int coinTotal;
     public Text coinText;
     public Text BuyOrPlayText;
  
  
     private void Awake ()
     {
  
         if (ES2.Exists ("unlocked.txt")) {
             gameObject.GetComponent ().unlocked = ES2.Load ("unlocked.txt");
  
         } else {
  
 //            gameObject.GetComponent ().unlocked = false;
  
         }
         coinText.GetComponent ().text = " Coins:" + coinTotal.ToString ();
     }
  
     void Start ()
     {
         { 
             //Get the money value stored
             if (ES2.Exists ("CoinsCollected.txt")) {
                 coinTotal = ES2.Load ("CoinsCollected.txt");
  
                 if (ES2.Exists ("unlocked.txt")) {
                     unlocked = ES2.Load ("unlocked.txt");
                 } else {
                     gameObject.GetComponent ().unlocked = false;
                 }
                 //Update the text for the money
                 coinText.GetComponent ().text = " Coins:" + coinTotal.ToString ();
             }
 //            //Save the money value stored
 //            ES2.Save (coinTotal, "CoinsCollected.txt");
 //            ES2.Save (unlocked, "purchasedCharacter.txt");
  
  
             if (gameObject.GetComponent ().unlocked == false) {
                 BuyOrPlayText.GetComponent ().text = "Buy";
 //                gameObject.GetComponent ().unlocked = false;
 //
 //                ES2.Save (coinTotal, "CoinsCollected.txt");
 //                ES2.Save (unlocked, "purchasedCharacter.txt");
  
             } else {
                 if (gameObject.GetComponent ().unlocked == true) {
                   
                     BuyOrPlayText.GetComponent ().text = "Play";
                     gameObject.GetComponent ().unlocked = true;
                     ES2.Save (coinTotal, "CoinsCollected.txt");
                     ES2.Save (unlocked, "unlocked.txt");
  
                 }
                 if (coinTotal <= 0 && gameObject.GetComponent ().unlocked == false) {
                   
                     coinTotal = 0;
                     gameObject.GetComponent ().unlocked = false;
                 } else {
                     if (coinTotal >= 0 && gameObject.GetComponent ().unlocked == false) {
                         gameObject.GetComponent ().unlocked = false;
  
                     }
                 }
             }
         }
     }
  
     // For Buying Levels
     public void BuyItem (String value)
     {
         ES2.Load ("unlocked.txt");
         if (coinTotal >= characterValue && gameObject.GetComponent ().unlocked == false) {
  
             coinTotal = coinTotal - characterValue;
 //            gameObject.GetComponent ().unlocked = true;
             unlocked = true;
             coinText.GetComponent ().text = " Coins:" + coinTotal.ToString ();
             BuyOrPlayText.GetComponent ().text = "Purchased!";
             ES2.Save (coinTotal, "CoinsCollected.txt");
             ES2.Save (unlocked, "unlocked.txt");
                   
         } else {
           
             if (coinTotal >= characterValue && gameObject.GetComponent ().unlocked == true) {
  
                 gameObject.GetComponent ().unlocked = true;
                 BuyOrPlayText.GetComponent ().text = "Play";
                 SceneManager.LoadScene (value);
                 ES2.Save (coinTotal, "CoinsCollected.txt");
                 ES2.Save (unlocked, "unlocked.txt");
           
             }
         }
               
         if (coinTotal <= characterValue && gameObject.GetComponent ().unlocked == false) {
           
             coinTotal = 0;
             gameObject.GetComponent ().unlocked = false;
             iTween.ShakePosition (gameObject, new Vector3 (9, 9, 0), 0.3f);
             ES2.Save (coinTotal, "CoinsCollected.txt");
             ES2.Save (unlocked, "unlocked.txt");
  
         } else {
           
             if (coinTotal <= 0 && gameObject.GetComponent ().unlocked == true) {
               
                 gameObject.GetComponent ().unlocked = true;
                 BuyOrPlayText.GetComponent ().text = "Play";
                 SceneManager.LoadScene (value);
                 ES2.Save (coinTotal, "CoinsCollected.txt");
                 ES2.Save (unlocked, "unlocked.txt");
             }
  
         }
     }
  
 }

 
Any help or guidance would be greatly appreciated. 

  • Liked by
Reply