Level counter gets stuck on the first phrase.
As you can tell by the title, I am making a level counter for a game. However, It always gets stuck on the first phrase which is "Tutorial". After that it is supposed to advance to "1" and then up from there. My code looks like this. On door to next level:
static var statLevel : int;
function OnTriggerEnter (){
if (statLevel == null)
{
statLevel = 1;
}
statLevel ++;
if (statLevel == 1)
{
Application.LoadLevel ("Challenge");
MainScript.level = "Tutorial";
}
if (statLevel == 2)
{
Application.LoadLevel ("MageChallenge1");
MainScript.level = "1";
}
if (statLevel == 3){
Application.LoadLevel ("MageChallenge2");
MainScript.level = "2";
}
if (statLevel == 4){
Application.LoadLevel ("MageChallenge3");
MainScript.level = "3";
}
if (statLevel == 5){
Application.LoadLevel ("MageChallenge4");
MainScript.level = "4";
}
if (statLevel == 6){
Application.LoadLevel ("MageChallenge5");
MainScript.level = "5";
}
if (statLevel == 7){
Application.LoadLevel ("MageChallenge6");
MainScript.level = "6";
}
if (statLevel == 8){
Application.LoadLevel ("MageChallenge7");
MainScript.level = "7";
}
if (statLevel == 9){
Application.LoadLevel ("FinalTest");
}
}
And in my main script:
static var deathLevel = "Challenge";
static var level : String;
public var deathMechanic : GameObject;
static var die = true;
static var isDying = false;
static function deathActivate (){
if (die == true)
{
if (deathLevel == "Challenge")
{
NextLevel.statLevel = 0;
level = "Tutorial";
}
if (deathLevel == "MageChallengeChallenge1")
{
NextLevel.statLevel = 1;
level = "1";
}
if (deathLevel == "MageChallengeChallenge2")
{
NextLevel.statLevel = 2;
level = "2";
}
if (deathLevel == "MageChallengeChallenge3")
{
NextLevel.statLevel = 3;
level = "3";
}
if (deathLevel == "MageChallengeChallenge4")
{
NextLevel.statLevel = 4;
level = "4";
}
if (deathLevel == "MageChallengeChallenge5")
{
NextLevel.statLevel = 5;
level = "5";
}
if (deathLevel == "MageChallengeChallenge6")
{
NextLevel.statLevel = 6;
level = "6";
}
if (deathLevel == "MageChallengeChallenge7")
{
NextLevel.statLevel = 7;
level = "7";
}
die = false;
Instantiate(activeDeathMechanic, player.position, Quaternion.identity);
yield WaitForSeconds (5);
Application.LoadLevel (deathLevel);
}
}
function Start ()
{
if (level == null)
{
level = "Tutorial";
}
die = true;
if (NextLevel.statLevel == 1)
{
level = "Tutorial";
}
if (NextLevel.statLevel == 2)
{
level = "1";
}
if (NextLevel.statLevel == 3)
{
level = "2";
}
if (NextLevel.statLevel == 4)
{
level = "3";
}
if (NextLevel.statLevel == 5)
{
level = "4";
}
if (NextLevel.statLevel == 6)
{
level = "5";
}
if (NextLevel.statLevel == 7)
{
level = "6";
}
if (NextLevel.statLevel == 8)
{
level = "7";
}
if (NextLevel.statLevel == 9)
{
level = "Final";
}
}
function Update (){
if (NextLevel.statLevel == 2){
deathLevel = "MageChallenge1";
}
if (NextLevel.statLevel == 3){
deathLevel = "MageChallenge2";
}
if (NextLevel.statLevel == 4){
deathLevel = "MageChallenge3";
}
if (NextLevel.statLevel == 5){
deathLevel = "MageChallenge4";
}
if (NextLevel.statLevel == 6){
deathLevel = "MageChallenge5";
}
if (NextLevel.statLevel == 7){
deathLevel = "MageChallenge6";
}
if (NextLevel.statLevel == 8){
deathLevel = "MageChallenge7";
}
} Can anyone give me a logical reason as to why this doesn't work and a way to fix it? Your help will be greatly appreciated.
I don't see where you are defining some of these variables, like "deathLevel". Is it giving compiler errors, or did you cut out some of the script declarations?
I do have those variables, I just forgot to copy them in when i asked my question. I receive no compiler errors.
Answer by Chris333 · Jun 04, 2016 at 10:14 AM
Hi,
int is a value-type, so its default value is 0, but you are checking the field statLevel of type int for null in the first if-clause of the first script. Null is the default value of reference types. Iam not sure if this also applies to js but in c# it works that way.
Yes, in all coding languages (not just Unity) integurs and floating points can't be null.
In js variables can be undefined. Thats why i was not sure if the declaration of the int-type above in unity script would cause the variable to have the same behaviour as in c# or other strict typed languages or just stay as undefined. Thx for clarifying that wolfrik.
I'm not 100% sure about that but I'm pretty sure.
Though I do know that for instance when making a private/static variable that's an array, it will have to be declared as "new Type[]", integurs do not have to be declared when they are private/static.
I have made some changes to account for this, but the level counter still gets stuck.
Answer by skillbow · Jun 04, 2016 at 10:20 AM
I think some of this code can be tightened up a bit logically. It might not sort your problem but the code is currently open to some logical problems, for instance change:
if (statLevel == null)
{
statLevel = 1;
}
to
if (statLevel <= 0)
{
statLevel = 1;
}
Also change your long if statements to else ifs as if OnTriggerEnter is triggered within a short space of time, there is the possibility that statLevel will be incremented before the end of the function.
Have you used breakpoints or Debug.Log(statLevel) throughout the functions to see where it's failing?
@skillbow I have added several Debug.Log statements to the code and all have triggered without fail. I placed one after every time I change the variable "level", although this still apparently stays at "Tutorial".
Where are you setting deathLevel. It would be much easier to debug your code if you reduce the size a bit, for instance the Start function could be reduced to something like:
die = true;
if (level =="" || NextLevel.statLevel == 1 ) {
level = "Tutorial";
} else if (NextLevel.statLevel == 9) {
level = "Final";
} else {
level = NextLevel.statLevel-1;
}
Also can you post the code for your NextLevel class?
NextLevel and $$anonymous$$ainScript are the names of the scripts that these variables are on. I am not sure if your code here would work, as level is a string and statLevel is an int, but I will give it a try.
Just do
int statLevelInt = NextLevel.statLevel -1;
level = statLevelInt.ToString();
to convert to a string.
Unfortunately, as I thought, this code did not work, even after quite a lot of modification.
Follow this Question
Related Questions
A certain string is causing trouble! (Javascript) 0 Answers
how to convert a dynamic playerpref to a dynamic int variable? 0 Answers
String Array = Anything? 1 Answer
Problem with Contains 0 Answers
PlayerPref doesnt work 2 Answers