How do I make my game change levels after collecting all objects?
I am very new to unity and I am having trouble with getting my game to change levels after I have collected all objects. I currently have a working main menu with buttons that allow me to exit and my first scene with an object collect system that displays score in the top right of my screen. // JavaScript var score = 0; function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "coin") { Debug.Log("Other object is a coin"); score += 1; Debug.Log("Score is now " + score); Destroy(other.gameObject); } } function OnGUI() { GUILayout.Label( "Score = " + score ); }
This code works perfectly for collecting the objects and counting score and this is what I am using to keep count.
I am wondering if anybody can help me work out a way so that when my score reaches a certain point, e.g... 3 points, my game will progress to the next level where I could create a more challenging terrain and harder to find collectables.
Any feedback is appreciated. Thanks
Answer by Kimi073 · Nov 20, 2015 at 07:54 AM
Hi,
I have two ways you may consider.
One is using Lerp which help you to change between multiple game object position and this is the way I use in most of my 2D games to change my screen (for example: from main menu to exit menu,..). For your case, this could be used to change between two objects which hold two different levels. However this needs a bit of practise to get over it well. But if you could, this would definitely your best tool.
[Link for Lerp documentation]: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
The second way, which I may recommend for beginner like you because it's what I think the easiest way is using "LoadLevel" which load another scene and in this case, load another level. I don't know how it's exactly written in JS (because I don't use it) but what here in CS:
//Your above code
//Put these lines of code in Update function
//You have to make a scene called "level2" or whatever you may want and put exactly in the quotation mark
if (score >= 3) {
Application.LoadLevel("level2");}
Sorry for my bad English. Feel free to ask further if you don't understand at any part. This is the first time I answer someone so may get the answer confusing
I'm not sure to undersand your first way, what are you lerping from and to ? If it's just about moving from a position to an other, you'd rather be using Vector3.$$anonymous$$oveTowards(); Using Lerp to move objects is a bad habit and a twisted way of using it ^^
But anyway, i do agree with your second way ! You can use the score variable or a objectCount at which every picked object would add 1 (in case that score and picking objects is not fully related)
In the first way, I add the main camera in a game object as a child and other screen in different game objects. Then I use Lerp to move the game object which contain the main camera to the game object which holds the right screen that I want.
LoadLevel() is the first method I learn and super easy and I really encourage using it ins$$anonymous$$d of Lerp if you don't want to learn too much at once.
@$$anonymous$$dRWaylander Ya thanks so much for your advise. I was trying to use $$anonymous$$oveToward() too but find Lerp kinda "fancier" to use because I can also make screen changing effect later on when I move to 3D. Anyways thanks so much :3