When the player starts the game a little tutorial pops up that in theory will not pop up again if player loses, the way I have made the restart button if player loses is that it just loads the scene back, but the tutorials also pops up, any idea how to fix this?
public static bool firstTime = true;
void Start()
{
if(firstTime)
{
lvl1.Setup();
firstTime = false;
}
}
lvl1 is the script for the UI of the tutorial I have tried to change the firstTime variable from lvl1 script but there is an error with something even though the bool is public, I don't know what to do, Thanks in advance!
EDIT: found the problem, the problem is that I had the UI background for the tutorial active by default, so even if the variable was false and it would not setup the UI, the UI would still be there every time I restart
CodePudding user response:
I think the problem is that when you restart the scene the boolean variable is recreated and set to true (default). To fix that check the following method for preventing GameObject destroy on scene reload:
CodePudding user response:
I think the best thing you can do, is to create static instance of your class when the game is loaded and then just check whether the instance is null or not. If it is null, create new instance and if it is not, then just keep the existing one and destroy new one.
public static "ClassName" instance;
private void Awake()
{
if(instance == null)
{
instance == this;
}
else
{
Destroy(gameObject);
}
}
and then you can use it as:
instance.firstTime
The good thing about this approach is that the public static instance can also be used in other scripts, without need to FindObjectOfType.