- Home /
on collision show gui
i got this script that should take you to another level when all of the objects are collected when entering this trigger, but i want it to show a text in screen when you didn't collect all the objects, anyone who can help??
Here's the current script:
// Get script from another object var itemTracker; itemTracker = GameObject.FindWithTag("GameController").GetComponent(GameStats);
// Number of the next level (can be found under File/Build Settings) var nextLevel = 3;
// If a collider is hit and all the objects are lower than 1 (0) then load the next leven function OnTriggerEnter (other : Collider) { if(itemTracker.wax < 1 && itemTracker.wood < 1 && itemTracker.feathers < 1) Application.LoadLevel(nextLevel); }
Answer by Raymond 2 · Mar 14, 2011 at 08:55 PM
I figured it out, i knew i needed a boolean but i then the message stayed there, but i got it working now, here's the final script for anybody who wants to do the same:
// Get script from another object var itemTracker; itemTracker = GameObject.FindWithTag("GameController").GetComponent(GameStats);
// Number of the next level (can be found under File/Build Settings) var nextLevel = 3;
//variable to check if all objects are collected var allCollected = false; var triggerHit = false; // If a collider is hit and all the objects are lower than 1 (0) then load the next level // When a collider is entered check if all the objects are collected and looad the next level function OnTriggerEnter (other : Collider) { triggerHit = true; if(itemTracker.wax < 1 && itemTracker.wood < 1 && itemTracker.feathers < 1) { Application.LoadLevel(nextLevel); allCollected = true; }
} // when the trigger is left, wait for 5 seconds, and set the triggerhit variable to false function OnTriggerExit (other : Collider) { yield WaitForSeconds(5); triggerHit = false; }
function OnGUI() { // if not all items are collected (allCollected = false;) and triggerhit is true then show a text in the middle of the screen wich says you have items to collect if(!allCollected && triggerHit) { GUI.Label (Rect (Screen.width/3, Screen.height/2, 600, 40), "You still have some objects to collect"); } }
EDIT: Added comments for clearity (just for those who don't understand and for documentation)
now you could accept your own answer! or so I hope. maybe you can only do it 2 days from now...
the bool was meant to turn on and off when you need it in this case the bool goes true in your on trigger enter on the respective else in your code and goes false on the true of that if i thought that part was common sense, still the answer works
yea i just had a brain dead moment so i didn't know what to do, i probably asked to soon, but anyway thanks for the help.
As for accepting my own answer, it takes 24 hours before i can do that.
Answer by poncho · Mar 14, 2011 at 08:46 PM
you need first have a class boolean variable that will decide when the GUI is showed then you need to assign it true when you want to show the GUI on the OnGUI function add the if(myboolean) then yourRespectiveGUI and that was all you needed