Okay so I have a big, annoying, problem.
I have working scripts. Here’s what my script does.
When you run over a coin it’ll pick it up and give you 100 points.
If you get 300 points it will load a new level.
What’s my problem?
Every time I want to make a new level I have to make a new script. Because:
if(points > 299)
Application.LoadLevel(1);
Since this is a set level, every time I want to go to a new level, I have to make a new script and change the level number.
Any way I can make a list of levels, and play them in order with only using one script?
Here’s my full code.
#pragma strict
var points = 0;
var player : Transform;
var pickupSound : AudioClip;
function OnCollisionEnter(player : Collision)
{
if(player.gameObject.name == "Coin")
points += 100;
audio.PlayOneShot(pickupSound);
}
public function OnGUI()
{
GUI.Label(Rect(10,10,100,30), points.ToString());
}
function Update()
{
if(points > 299)
Application.LoadLevel(1);
}
Alternatively, you could have used variables for the number of coins and level to go to, and expose those variables in the editor. Then that same script works no matter how many coins they need and what level you want them to go to.
Agreed with the above. What I do is make a general level load script (actually, it’s a main menu+world menu load script as well) and input data into the inspector. I then use strings to match the names of the scene’s name, so I don’t have to worry at all about the actual scene number (which could easily change).
You can always make a public string, maybe called “nextLevel”, and then use Application.LoadLevel(nextLevel)