- Home /
Level Compete Spot
Okay, I am currently using these two scripts to tell my game when to end the level. What do I do to make it so that the script comes into play when the character touches a certain area?
var message: Texture2D; //you put text in here, that you made in photoshop, the "you win!" type deal var button: Texture2D; //this would be text, that you made in photoshop, that says next level var nCrystals = 0; //starting number of crystals var neededCrystals = 10; //number needed. Just change this in the editor var nextLevel = 0; //this would be the # of the next level, if you use it
function OnTriggerEnter( hit : Collider ) {
if (nCrystals >= neededCrystals) {
GUI.DrawTexture(Rect((Screen.width-message.width)/2, 200, message.width, message.height), message);
if (GUI.Button(Rect((Screen.width-button.width)/2, 400, button.width, button.height), button))
{
Application.LoadLevel(nextLevel);
} } }
And this one
var explosion : Transform; //this will be the particle, i.e. fireworks var controlScript : LevelCompleted; // drag here the object that has the LevelCompleted script static var crystal = 0; //this isn't used anymore in this script static var point = 0; //how many points you receive var playFireworks = false; // this isn't really nescessary
function OnTriggerEnter(other: Collider) { if (other.tag == "wormy") //This is what your character is tagged as. You have to create a tag and tag it as this //or create your own tag and change it in this script { controlScript.nCrystals++; //This is the level complete script Destroy(gameObject); //this causes the crystals to self destruct playFireworks = true; //states that fireworks will come out of the crystal when it's collected
if (playFireworks == true)
{
var explosion = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
crystal += 1;
point += 5000;
playFireworks = false;
}
if (playFireworks == false)
{
Destroy(gameObject);
}
}
}
Hey Hunter, are you asking how to implement these in your game or are these scripts giving you an error or perhaps not throwing a collision?
The top script was working fine when it was function OnGUI() but then when I changed it to function OnTriggerEnter it hasn't been working. I thought changing it would make it so that when I attached it to a cube it would do the same thing when the character collided with it but it doesn't.
Answer by torrente · Jul 03, 2012 at 03:01 AM
I see. Yes, the Gui needs to be put back in to function OnGUI(), so put your two GUI items back there. Then, add in a boolean that is set to true when the collision is fired, something like this:
var message: Texture2D; //you put text in here, that you made in photoshop, the "you win!" type deal var button: Texture2D; //this would be text, that you made in photoshop, that says next level var nCrystals = 0; //starting number of crystals var neededCrystals = 10; //number needed. Just change this in the editor var nextLevel = 0; //this would be the # of the next level, if you use it //ADD THE NEXT LINE /////////////////////////// var levelComplete = false;
//CHANGE THIS TO SOMETHING LIKE THIS ///////////////////////// function OnTriggerEnter( hit : Collider ) {
if (nCrystals >= neededCrystals) {
levelComplete = true; } }
// ADD THE FOLLOWING ////////////////////////////////////// function OnGUI() {
if(levelComplete) { //PUT YOUR LEVEL COMPLETE GUI HERE /////////////////////// } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613073611im_/https://answers.unity.com/themes/thub/images/avi.jpg)