- Home /
Load a new level after objects have been collected
Hi,
I am making a simple game and I am a unity beginner. In my game the player must go around the maze and collect coins to get a score. So far this part is working. I have a chest in the level and when the player reaches the chest they move up to the next level. However, I would like to make sure the player has collected all the coins before the chest moves the player to the next level. Otherwise the player can skip collecting the coins and go straight to the next level.
So far I have this script attached to my player which is counting the collected coins:
var score = 0;
var guiScore : GUIText;
var nextLevel : boolean;
function Start (){
guiScore.text = "Score : 0";
}
function OnTriggerEnter(other : Collider){
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin"); {
score += 5;
guiScore.text = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function AddScore(){
score++; if( score == 20 ){ Application.LoadLevel(2); } }
Then this is the script attached the the chest:
public class : MonoBehaviour
{
void Awake() {
ExitObj.consInLevel++;
function OnMouseDown (){
Application.LoadLevel(2);
}
I have no idea what I'm doing. Please help.
Answer by Yofurioso · Mar 18, 2014 at 12:52 PM
You should have an int variable on the player script to keep track of how many coins he has picked up, another int value on the chest script to recieve the coins from the player and if statement to check if the amount of coins is right:
var score = 0;
var guiScore : GUIText;
var nextLevel : boolean;
var coinsCollecter : int;
function Start (){
guiScore.text = "Score : 0";
}
function OnTriggerEnter(other : Collider){
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin"); {
score += 5;
coinsCollected ++;
guiScore.text = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
chest script:
public class : MonoBehaviour
{
public int coinsInChest = 0;
void Awake()
{
ExitObj.consInLevel++;
}
public void OnMouseDown()
{
coinsInChest += GameObject.FindGameObjectWithTag("Player").coinsCollected;
GameObject.FindGameObjectWithTag("Player").coinsCollected = 0;
if(coinsInChest >= 100) //put the amount you want here
{
Application.LoadLevel(2);
}
}
}
By the way, I've noticed that the first script is js and the second is c#, you may want to keep your scripts on the same ¿language?. Hope it helps
Answer by TheMannyzaur · Mar 18, 2017 at 01:46 PM
how about u create a bool to check if everything is collected. once it is you activate the chest. get it?
Your answer
Follow this Question
Related Questions
Asset Bundle starts loaded 0 Answers
Loading a level and getting all new gameobjects 1 Answer
How to destroy a gameobject on collision and keep it dead on reload of the scene? 1 Answer
survival shooter game restarting when player dies 2 Answers
How do I stop the scene from switching when the gameObject is destroy / 0 Answers