Unlocking Levels through Collectibles

Hi guys,

I need levels to unlock depending on how many collectibles have been gathered. However, the levels originally unlocked just by completing a level, now I would rather have them unlock only once you have collected a certain amount of balls. It still works as normal without taking into account the ballsUnlock variable.

Here is the code:

ballsUnlock is the public int I’ve set to allow me to manually input the amount of collectibles (balls) collected by the player.

I have highlighted the part which may be incorrect in Red.

Hope you can help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapPoint : MonoBehaviour
{
public MapPoint up, right, down, left;
public bool isLevel, isLocked;
public string levelToLoad, levelToCheck, levelName;
public int gemsCollected, totalGems, ballsUnlock;
public float bestTime, targetTime;
public GameObject gemBadge, timeBadge;
// Start is called before the first frame update
void Start()
{
if (isLevel && levelToLoad != null)
{
if (PlayerPrefs.HasKey(levelToLoad + “_gems”))
{
gemsCollected = PlayerPrefs.GetInt(levelToLoad + “_gems”);
}
if (PlayerPrefs.HasKey(levelToLoad + “_time”))
{
bestTime = PlayerPrefs.GetFloat(levelToLoad + “_time”);
}
if (gemsCollected >= totalGems)
{
gemBadge.SetActive(true);
}
if (bestTime <= targetTime && bestTime != 0)
{
timeBadge.SetActive(true);
}
isLocked = true;
if (levelToCheck != null)
{
if (PlayerPrefs.HasKey(levelToCheck + “_unlocked”))
{
if (PlayerPrefs.GetInt(levelToCheck + “_unlocked”) == 1)
{
if (gemsCollected >= ballsUnlock)
{
isLocked = false;
}
}
}
if (levelToLoad == levelToCheck)
{

{
isLocked = false;
}
}
}
// Update is called once per frame
void Update()
{
}
}
}
}

It’s hard to read your code and color coding it all blue didn’t help.

1 Like

With code tags, please use them next time for readability:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapPoint : MonoBehaviour
{
public MapPoint up, right, down, left;
public bool isLevel, isLocked;
public string levelToLoad, levelToCheck, levelName;
public int gemsCollected, totalGems, ballsUnlock;
public float bestTime, targetTime;
public GameObject gemBadge, timeBadge;
// Start is called before the first frame update
void Start()
{
if (isLevel && levelToLoad != null)
{
if (PlayerPrefs.HasKey(levelToLoad + "_gems"))
{
gemsCollected = PlayerPrefs.GetInt(levelToLoad + "_gems");
}
if (PlayerPrefs.HasKey(levelToLoad + "_time"))
{
bestTime = PlayerPrefs.GetFloat(levelToLoad + "_time");
}
if (gemsCollected >= totalGems)
{
gemBadge.SetActive(true);
}
if (bestTime <= targetTime && bestTime != 0)
{
timeBadge.SetActive(true);
}
isLocked = true;
if (levelToCheck != null)
{
if (PlayerPrefs.HasKey(levelToCheck + "_unlocked"))
{
if (PlayerPrefs.GetInt(levelToCheck + "_unlocked") == 1)
{
if (gemsCollected >= ballsUnlock)
{
isLocked = false;
}
}
}
if (levelToLoad == levelToCheck)
{

{
isLocked = false;
}
}
}
// Update is called once per frame
void Update()
{
}
}
}
}

ok, not sure why it’s not formatting correctly now either.
I’d recommend not doing as many nested if() statements, combine them with &&:

if (levelToCheck != null)
{
if (PlayerPrefs.HasKey(levelToCheck + "_unlocked"))
{
if (PlayerPrefs.GetInt(levelToCheck + "_unlocked") == 1)
{
if (gemsCollected >= ballsUnlock)
}
}
}

becomes

if(this && that && theOther && theLastThing)
{ }

That might help with scope issues.

sorry guys, will tidy it up next time, I just slapped it on here without a second thought to be honest. The blue was facepalm worthy I admit - lol . Will tidy up the code a little more and see what I can come up with. Will get back to you, will be working on this today.

Hi, did you solve your problem? I have the same problem, i wanna unlock the levels with certain amount of collectibles.