- Home /
What is a better way of storing information for levels?
I want to change some variables when entering different levels, what I currently have is this but I bet there is a better way for me to do this;
switch(level){
case 1:
s= 10;
m= 10;
break;
case 2:
s= 20;
m= 30;
break;
etc....
There are many ways to do this... One direct way is to declare the variables as static and then use dontdestroyonload for that object... Or also probably use singleton classes .... Or another way might be playerprefs depending on what is the nature of variables ...
Red
The game runs a level depending on that level the s and m variables change, that's all that's gotta happen but the way I do it now in a script that does a few other things it looks very messy and long so I was wondering if there would be a more efficient or cleaner way, I'll have a look into those 2 and see what I can do, thanks for the reply.
You may also consider, in that case to use ... Application.Isloadedlevel or application.loadedlevel with static variables and dontdestroyonload .... To have variables state depending on level loaded..
Red
This looks very interesting and I've never used it before, I'll go ahead and give it a try, thanks a lot!
Answer by NoseKills · Dec 08, 2014 at 09:54 AM
If the level data is as simple as you describe, efficiency wise it doesn't really matter much. I just usually make an array of the data into it's own class in this type of cases.
Something like this: (sorry i didn't write this in Mono so there might be obvious spelling errors in this code)
public class LevelData
{
public int _S, _M;
public LevelData(int s, int m)
{
_S = s;
_M = m;
}
}
public static class Levels
{
public static LevelData[] levels;
// static constructor
static LevelData()
{
levels = new LevelData[]
{
new LevelData(10,10),
new LevelData(20,30),
}
}
public static LevelData getLevel(int levelNr)
{
return levels[levelNr-1];
}
}
Your answer

Follow this Question
Related Questions
I need a script for an FPS game. 1 Answer
Does the quality settings remain when you move level? 1 Answer
level up need help 1 Answer
How to add new level? 1 Answer
Loadlevelasync - Application Hangs 2 Answers