- Home /
How to get random GUI to appear, on entering a trigger. (PIC INCLUDED)
So i'm trying to make random gui's appear when the player enters a trigger. I'm not sure if random gui's are the right things to use for this. If anyones played Mount and Blade i'm trying to make it like that were theres a description of some stuff thats happening overlayed on a picture, and then several options that you can choose from to respond to what is happening.
I know this script doesn't work I just made it to be a rough idea of what i'm trying to do.
using UnityEngine; using System.Collections; public class DetectGridGrassland : MonoBehaviour { void OnTriggerEnter(Collider other) { Debug.Log("Player entered the Grass Land"); float rnd = Random.value; if ( rnd < 0.05 ) { GUI.Box (Rect (10,10,100,90), "Attacked by bandits"); } else if ( rnd < 0.15 ) { GUI.Box (Rect (10,10,100,90), "Attacked by wolves"); } else { GUI.Box (Rect (10,10,100,90), "Nothing happens"); } } } }
Here is what i'm trying to achieve.
I could also maybe instantiate the picture in front of the camera and then overlay the buttons over the choices or something maybe?
Answer by Stormizin · Feb 20, 2014 at 08:03 PM
Try something like that actually in C#:
public int randNum = 0;
void Start(){
randNum = Random.Range(0,5);
}
void OnGUI(){
if (randNum <= 3){
GUI.Button(new Rect(10, 10, 125, 25), "Attacked by bandits");
}
else if(randNum == 3){
GUI.Button(new Rect(10, 10+20, 125, 25), "Attacked by wolves");
}
else{
GUI.Button(new Rect(10, 10+30, 125, 25), "Nothing to do!");
}
}
You can also do it with an array, it's a cleaner code and better to control.
Your answer
Follow this Question
Related Questions
triggering random animations with gui 1 Answer
Why can't void be used in this context? 2 Answers
Can I set a specific Pointer for the EventTrigger on Buttons for example? 0 Answers
How do I use a trigger to change the GUIText 2 Answers
Clicking an Object on IPAD when within a Certain Distance 0 Answers