Funny Problem maybe a bug!

Ok so i have this bad script but it works fine the most of the time at least ,the thing is that the public void GoldA() its not working properly i have tried everything but every time it enters this void the GoldLock become 0 i put a text so ican see the number of the goldlock before enter the “if” and in my screen it says 15 when i enter to GoldLock become 0 the funny part is i used the same tactic for all the other voids but they work just fine the only difference is that the playerpref is set in another scene

i try to change the name o the playerpref …didnt work
i double check everything they look fine
i know that my code is bad but it used to work fine

my english is not my native language i hope you will understand what im trying to say !
anyway thanks for your time!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ColourSetA : MonoBehaviour
{
    public GameObject Green;
    public GameObject Black;
    public GameObject White;
    public GameObject Blue;
    public GameObject Pink;
    public GameObject Red;
    public GameObject Gold;
    public GameObject WhitePlay;
    public GameObject BlackPlay;
    public GameObject GreenPlay;
    public GameObject BluePlay;
    public GameObject PinkPlay;
    public GameObject RedPlay;
    public GameObject GoldPlay;
    public GameObject BlackLockButton;
    public GameObject WhiteLockButton;
    public GameObject BlueLockButton;
    public GameObject PinkLockButton;
    public GameObject RedLockButton;
    public GameObject GoldLockButton;
    public GameObject PriceBlack;
    public GameObject PriceWhite;
    public GameObject PriceBlue;
    public GameObject PricePink;
    public GameObject PriceRed;
    public GameObject PriceGold;
    private int WhiteLock;
    private int BlackLock;
    private int BlueLock;
    private int PinkLock;
    private int RedLock;
    private float GoldLock;
    public Text debug;
   
    void Update()
    {
        GoldLock = PlayerPrefs.GetInt("Fgold");
        RedLock = PlayerPrefs.GetInt("Redlock");
        BlueLock = PlayerPrefs.GetInt("Bluelock");
        BlackLock = PlayerPrefs.GetInt("blacklock");
        WhiteLock = PlayerPrefs.GetInt("Whitelock");
        PinkLock = PlayerPrefs.GetInt("Pinklock");
        if (GoldLock > 0)
        {
            PriceGold.SetActive(false);
        }
        if (GoldLock <= 0)
        {
            PriceGold.SetActive(true);
        }
        if (BlueLock > 0)
        {
            PriceBlue.SetActive(false);
        }
        if (BlueLock <= 0)
        {
            PriceBlue.SetActive(true);
        }     
        if (BlackLock <= 0)
        {          
            PriceBlack.SetActive(true);
        }
        if (BlackLock > 0)
        {
            PriceBlack.SetActive(false);
        }
        if (WhiteLock <= 0)
        {  
            PriceWhite.SetActive(true);
        }
        if (WhiteLock > 0)
        {
            PriceWhite.SetActive(false);
        }
        if (PinkLock > 0)
        {
            PricePink.SetActive(false);
        }
        if (PinkLock <= 0)
        {
            PricePink.SetActive(true);
        }
        if (RedLock > 0)
        {
            PriceRed.SetActive(false);
        }
        if (RedLock <= 0)
        {
            PriceRed.SetActive(true);
        }
    }
    public void GreenA()
    {
        Gold.SetActive(false);
        Red.SetActive(false);
        Pink.SetActive(false);
        Blue.SetActive(false);
        Green.SetActive(true);
        Black.SetActive(false);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(true);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        GoldLockButton.SetActive(false);
    }
    public void BlackA()
    {
        Gold.SetActive(false);
        Red.SetActive(false);
        Pink.SetActive(false);
        Blue.SetActive(false);
        Green.SetActive(false);
        Black.SetActive(true);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(true);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        GoldLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        BlackLock = PlayerPrefs.GetInt("blacklock");
        if (BlackLock <= 0)
        {
            BlackLockButton.SetActive(true);
            BlackPlay.SetActive(false);

        }
        if (BlackLock > 0)
        {
            BlackLockButton.SetActive(false);
           
        }

    }
    public void WhiteA()
    {
        Gold.SetActive(false);
        Red.SetActive(false);
        Pink.SetActive(false);
        Blue.SetActive(false);
        Green.SetActive(false);
        Black.SetActive(false);
        White.SetActive(true);
        WhitePlay.SetActive(true);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        GoldLock = PlayerPrefs.GetInt("Whitelock");
        if (WhiteLock <= 0)
        {
            WhiteLockButton.SetActive(true);
            WhitePlay.SetActive(false);
         
        }
        if (WhiteLock > 0)
        {
            WhiteLockButton.SetActive(false);
          
        }
    }
    public void BlueA()
    {
        Gold.SetActive(false);
        Red.SetActive(false);
        Pink.SetActive(false);
        Blue.SetActive(true);
        Green.SetActive(false);
        Black.SetActive(false);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(true);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        GoldLockButton.SetActive(false);
        BlueLock = PlayerPrefs.GetInt("Bluelock");
        if (BlueLock <= 0)
        {
            BlueLockButton.SetActive(true);
            BluePlay.SetActive(false);

        }
        if (BlueLock > 0)
        {
            BlueLockButton.SetActive(false);

        }
    }
    public void PinkA()
    {
        Gold.SetActive(false);
        Red.SetActive(false);
        Pink.SetActive(true);
        Blue.SetActive(false);
        Green.SetActive(false);
        Black.SetActive(false);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(true);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        GoldLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        PinkLock = PlayerPrefs.GetInt("Pinklock");
        if (PinkLock <= 0)
        {
            PinkLockButton.SetActive(true);
            PinkPlay.SetActive(false);

        }
        if (PinkLock > 0)
        {
            PinkLockButton.SetActive(false);

        }
    }
    public void RedA()
    {
        Gold.SetActive(false);
        Red.SetActive(true);
        Pink.SetActive(false);
        Blue.SetActive(false);
        Green.SetActive(false);
        Black.SetActive(false);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(true);
        GoldPlay.SetActive(false);
        WhiteLockButton.SetActive(false);
        GoldLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        PinkLock = PlayerPrefs.GetInt("Redlock");
        if (RedLock <= 0)
        {
            RedLockButton.SetActive(true);
            RedPlay.SetActive(false);

        }
        if (RedLock > 0)
        {
            RedLockButton.SetActive(false);

        }
    }
    public void GoldA()
    {
        Gold.SetActive(true);
        Red.SetActive(false);
        Pink.SetActive(false);
        Blue.SetActive(false);
        Green.SetActive(false);
        Black.SetActive(false);
        White.SetActive(false);
        WhitePlay.SetActive(false);
        BlackPlay.SetActive(false);
        GreenPlay.SetActive(false);
        BluePlay.SetActive(false);
        PinkPlay.SetActive(false);
        RedPlay.SetActive(false);
        GoldPlay.SetActive(true);
        WhiteLockButton.SetActive(false);
        RedLockButton.SetActive(false);
        BlackLockButton.SetActive(false);
        PinkLockButton.SetActive(false);
        BlueLockButton.SetActive(false);
        GoldLock = PlayerPrefs.GetInt("Fgold");
        debug.text = GoldLock.ToString();
        if (GoldLock <= 0)
        {
            GoldLockButton.SetActive(true);
            GoldPlay.SetActive(false);
            Debug.Log("why you are here again");
        }
        if (GoldLock > 0)
        {
            GoldLockButton.SetActive(false);

        }
    }
}

I’m confused by your explanation. It sounds like you are saying that GoldLock has a value of 15 on line 315 when you convert it to a string and display it as text, but then you see the Debug.Log message on line 320 implying that “GoldLock <= 0” returned true on line 316. But that seems impossible, so I think I must not be understanding your description.

But some stuff I notice:

In line 178, you are setting the GoldLock variable equal to PlayerPrefs “Whitelock”. That seems likely to be an error, though I’m not sure whether it’s causing your current troubles.

Similarly, your red function is using the pink variable.

Calling PlayerPrefs.GetInt every frame in Update seems like a bad idea, performance-wise. PlayerPrefs is designed to remember stuff when the player quits the game and then comes back later. Data that you are using regularly while the game is running should generally be stored in regular variables instead.

1 Like

First of all thank a lot for your time i fix the line 178 and the pink variable and that fix i my problem second i dont know how but the debug.text had value 15 but now belong to past seems that the line 178 was the root of my problem
thank a lot good sir!

So the main thing here is that, there’s probably a simple small thing that was copied and pasted wrong, but finding that one thing in your big pile of repetitive code is going to be a nightmare. It looks like you know this is an area that needs improvement, so if you want to rewrite the script to make it much easier to get a grip on, here’s some advice. (Rewriting the script will probably fix all the current problems you have, but then give you a new set of exciting problems - but with a shorter, less repetitive script, finding and fixing these problems will be easier, and you won’t have to do it nearly as much)

Also, all of this should be done in addition to other advice, such as the PlayerPrefs advice from Antistone above - that is good advice for sure.

First: Classes and arrays! It looks like you have 6 kinds of things (colors) that all have the same type of information associated with them (main object, “play”, lock button, price, a “lock” value, and I’ll add one that will make sense as we go, the name). Lines 8 through 39 can be replaced with a single class definition and array declaration totaling about 8 lines of code:

[System.Serializable]
public class ColorThing {
public string name;
public GameObject mainObject;
public GameObject playObject;
public GameObject lockButton;
public GameObject price;
public int lock;
}
public ColorThing[] allColors;

(System.Serializable makes the class editable in the inspector, which you clearly want to do here)
So now you can go into this in the inspector, and set up your 6 colors with all the same object links you had before, but now, they’re in a much better data structure and you will need MUCH less copy-pasted code.

So now, anywhere you’ve had long strings of copy-pasted lines before, you’ll want to use for loops instead. These will loop through the array we created above, and you can run the same code on those items. Plus, you can use a function parameter that matches your color name to reduce the number of functions too! All of your BlackA, WhiteA, etc function will be replaced with a single, much shorter function, that will look something like this: (I’m assuming “A” means “Activate”, but feel free to rename the function of course)

void ActivateColor(string colorNameToActivate) {
for (int c=0; c < allColors.Length; c++) {
bool doActivate = (allColors[c].name == colorNameToActivate);
allColors[c].mainObject.SetActive(doActivate);
allColors[c].playObject.SetActive(doActivate);

if (doActivate) {
allColors[c].lock = PlayerPrefs.GetInt(allColors[c].name+"lock");
allColors[c].playObject.SetActive(allColors[c].lock > 0);
allColors[c].lockButton.SetActive(allColors[c].lock <= 0);
}
else {
allColors[c].lockButton.SetActive(false);
}
}

//So now, instead of calling BlackA(), you'll instead write:
ActivateColor("black"); // where the string here matches the name you've set up in the inspector for that color

And that’s pretty much it. That one function covers all the stuff you’re doing in lines 99-327, and does fundamentally the same stuff as far as I understand your functions to work.

So using this as a starting point, you just have Update remaining to write, but I hope that the example of the loop I gave above will be enough to help you figure out how to write that, and it’ll be good practice for learning to understand the concepts I’ve shown here. :slight_smile: I will say that you shouldn’t need more than about 10 lines of code to make that work in this new structure.

2 Likes

Wow…That was really helpfull!! Thanks a lot