[c# Scripting] Unlocking levels with coins confusion.

Hi
Would really like your help with this code… I am a Unity / C# noob, so please forgive the hot mess of code you are about to see.

I want to be able to unlock my levels with coins and iap… I have been following a few tutorials but have messed this up.
I don’t think the levels are Locking and unlocking, and they subtracting again when button is pressed on entering of the level menu.

I want to be able to:

  1. OnClick, unlock and buy this level/Character and change the display text from Buy to Play then,
  2. When you press the same button, you no longer subtract point, but you can press to go to that level.

I have tried the Instantiate Buttons…but i like the dynamics on the scroll rect I have soI am keeping the buttons placed already in the scene

Would be so greatful if anyone could tell me what i am doing wrong here. 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 int unlocked;
    public bool IsInteractable;
    public static bool purchasedCharacter =false;
    public int characterValue;
    public int coinTotal;
    public Text coinText;
    public Text BuyOrPlayText;


    private void Awake ()
    {
        coinText.GetComponent<Text>().text = " Coins:" + coinTotal.ToString();
    }

    void Start ()
    {
        { 
            GetComponent<Button> ().onClick.AddListener (() => BuyItem ("Level" + LevelText.text));

            //Get the money value stored
            if (ES2.Exists ("CoinsCollected.txt"))
                {
                coinTotal = ES2.Load<int> ("CoinsCollected.txt");

            if (ES2.Exists ("purchasedCharacter.txt"))
                   
                {
                ES2.Load<int> ("purchasedCharacter.txt");
                }

                //Update the text for the money
                coinText.GetComponent<Text> ().text = " Coins:" + coinTotal.ToString ();
            }
            //Save the money value stored
            ES2.Save (coinTotal, "CoinsCollected.txt");


            if (unlocked == 0)
            {
                BuyOrPlayText.GetComponent<Text> ().text = "Buy";

            } else
            {
                if (unlocked == 1)
                {
                    BuyOrPlayText.GetComponent<Text> ().text = "Play";

                }


           
                if (coinTotal <= 0 && unlocked == 0)
                {
                    coinTotal = 0;
                    myButton.interactable = false;
                }
                else
                {
                    if (coinTotal >= 0 && unlocked == 0)
                    {
                        myButton.interactable = true;

                    }
                }
            }
        }
    }


    public void BuyItem (String value)
            {
    if (coinTotal >= characterValue && unlocked == 0)
                {

                    coinTotal = coinTotal - characterValue;
                    unlocked = 1;
                    coinText.GetComponent<Text> ().text = " Coins:" + coinTotal.ToString ();
                    BuyOrPlayText.GetComponent<Text>().text ="Purchased!";
                   
                   
                    ES2.Save<int> (coinTotal, "CoinsCollected.txt");
                    ES2.Save <int> (unlocked, "purchasedCharacter.txt");
                   
            }
        else
            {
           
    if (coinTotal >= characterValue && unlocked == 1) {
                    unlocked = 1;
                    BuyOrPlayText.GetComponent<Text> ().text = "Play";
                    SceneManager.LoadScene (value);
           
                       
                    }
                }
               
    if (coinTotal >= characterValue && unlocked == 0) {   

                    ES2.Save<int> (coinTotal, "CoinsCollected.txt");
                    ES2.Save <int> (unlocked, "purchasedCharacter.txt");

            }
        else
            {
           
    if (coinTotal <= 0 && unlocked == 1)
            {

                    unlocked = 1;
                    BuyOrPlayText.GetComponent<Text> ().text = "Play";
                    SceneManager.LoadScene (value);
                    ES2.Save<int> (coinTotal, "CoinsCollected.txt");
                    ES2.Save <int> (unlocked, "purchasedCharacter.txt");
            }

        }
    }

}

Could you perhaps cite the tutorials that you were using?

Sure :slight_smile:

https://www.youtube.com/watch?v=a0fYMnurBUk

https://www.youtube.com/watch?v=xSDfSDTtUMs

https://www.youtube.com/watch?v=-cTgL9jhpUQ

Most of them unlock levels by completing the last level.
T thing i’m getting lost because I want to unlock levels similar to a character unlock system using coins and eventually iap.
Thanks

lol bloody hell, no wonder you’re getting lost and your code is a mess you’re picking and choosing code from 3 different tutorials which will be using different methods to save and load.

First things first, what type of unlock system do you actually want? Are you happy with something simple where it just unlocks the game levels as you progress? Or do you want something more complicated that saves to a text file for the future and so on or do you want a full on saving and loading screen?

If you want just purely a simple unlock system then I would use this tutorial which I’ve seen myself.

Go through it and get the code done from scratch so I and everyone else knowswhat you’re up to or you can pick another one, but pick one. If you’re new that explains why things are going wrong, you seem to be combining bits of code together but you don’t really know what that code does because I’m looking at it myself now and it’s baffling me a bit.

If you want to debug this a bit, are there any text files even appearing with the numbers you’re saving to them? That’s one thing you can check out right away, I know saving and loading can get pretty crazy as I’ve discussed it all with the more experienced programmers here awhile back, the worst thing you can do is fiddle with different methods of saving and loading without knowing what you’re doing you’ll just get frustrated.

Yeah I hear you! lol

Started off fine… Everything actually works in regards to coin subtraction, level selected ect,
It just doesn’t save the “unlocked”
I really want something more complicated that saves to a text file for the future (lol The noob wants it all)

You are right, I will try and start from scratch :frowning:
Thank you for taking the time to help me.

Well I wouldn’t recommend that right now as you’re only doing level unlocks I wouldn’t think it was necessary, however for future reference there’s actually a topic I made where I bickered with programmers for a few months about saving and loading and those conversations helped me out a lot and there was this guy HiddenMonk who helped me out with a lot of the actual code with some people posting.

I always keep this thread handy for newbies who want to learn saving and loading because the information there is all up to date, saving to files and stuff is pretty tricky, stick with the basics for now and work your way up.

Thank you so much!
will also check out the thread.

Thank you

You’re not really assigning anything to unlocked variable before calling BuyItem.
Maybe you should load and assign the unlocked value from your save file like you’ve done with coinTotal in your start?

1 Like

Thank you @Vipsu !!
That is the part where the unlocked was not saving sorted!!! Thank you :slight_smile:

The only thing that is happening now is all level buttons are sharing the same unlocked int variable and not unlocking separately OnClick

@Lethn the Thread is very interesting, I am going to get stuck in to learning from it also.

The way you’re saving looks pretty odd, it’s like you’re saving everything to a separate file.
After quick googling found something like this, maybe you could save variables instead?

ES2.Save(123,  "myInt");

/* Check that there is data to load */
if(ES2.Exists("myInt"))
    /* Load the int we saved into myInt */
    myInt = ES2.Load<int>("myInt");

Haven’t used “Easy save 2” myself though so can’t really help with that.

using System.Security.Cryptography.X509Certificates;

As side note, encrypted save files won’t save your game from memory hacking. There are ways for people to browse and edit memory on runtime. Meaning they could just search the value of coinTotal and unlocked from memory and edit them to whatever.

1 Like

Great video.

Now the noob wants to know what is the safest and most secure for my soon to be multimillion earning game lol…but I will take it slow.

Would you suggest binary formatter?