Collect Objects to Win Level
1). Things needed - a character controller, a collectible item (some sort of coin - you can have more than one of them), a particle effect that your coin turns into, a portal to your next level, and a new level to travel to.
2). Tagging and Istrigger - Tag the player as Player and check Istrigger on the coin (you should only have one coin at this point)
3). There are two scripts for this assignment - collect and levelChange. These two scripts MUST BE NAMED collect AND levelChange (caps matter).
var keyReplacement: GameObject;
var callLevelScript:levelChange;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player"){
callLevelScript.amountOfKeysCollected++;
//make the replacement variable show up
var makeKeyReplacement : GameObject;
makeKeyReplacement = Instantiate(keyReplacement, transform.position, transform.rotation);
//destroy the key and replace it with sparks
var amountOfKeysCollected:int = 0;
var numberOfKeysToChangeLevel:int = 3;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player"){
if(amountOfKeysCollected == numberOfKeysToChangeLevel){
Application.LoadLevel ("levelname");
MAKE SURE YOU CHANGE THE NAME OF YOUR LEVEL FROM "levelname" TO WHATEVER THE NAME IS FOR YOUR SECOND LEVEL.
4). Adding the scripts - Drag the levelChange script onto your portal. Drag the collect script onto your key.
5). Adding the other variables - The collect script has two variables: a Key Replacement and a Call Level Script. Put your particle effect in the first slot (the Key Replacement), and drag your portal object into the other slot.
6). Changing the number of keys - you can now copy the keys and make as many as you want. There is a set number of keys needed to open the portal for the next level. It is set at 3 by default. Modify the numberOfKeysToChangeLevel variable according to how many keys you have.
7). Build the project and run it.