- Home /
Choose which GUItext to display
Hi, it's me again.
So I've got a treasure chest. Several of them, actually, and all of them contain different amounts of gold. Decided to make a script where I could enter how much gold in the inspector. I also have a script attached that displays a GUItext when you open the chest, giving the amount of gold you just earned. However, it appears to my eyes that the GUItext is actually combining the amounts from ALL the chests with that script. (so that it always overlap the amount of gold - kir in this game - from ALL the chests with that script attached) Am wondering if anyone knows a solution to that. Bonus round: I'm sure there's a way to combine the scripts, but every time I try to run OnGui from within the openchest script, I never trigger the GUI, so good on ya if you're able to help this noob figure out how to do this in one script. Thanks, and God bless.
Script 1: Chest open
var chestSound : AudioClip; //chest opening sound clip goes here
var treasureChest : GameObject; //treasure chest prefab goes here
var moneyinchest : int;
// function Start(){
//if(PlayerPrefs.GetInt("Opened Chest", 0) == 1){
//Destroy(gameObject);
// }
// }
function OnTriggerEnter (col : Collider) {
//PlayerPrefs.SetInt("Opened Chest", 1);
if(col.gameObject.tag == "Player") { //checks to see that our character controller with tag "Player" has entered the trigger
Playermoney.curMoney += moneyinchest;
openmoneychestgui.openchest = true;
AudioSource.PlayClipAtPoint(chestSound, transform.position); //plays our soundclip at position of collider/trigger
treasureChest.animation.Play(); //plays the default animation applied to our treasureChest model
Destroy(gameObject);// destroys the gameobject that has this script, so our collider in this case
}
}
Script 2: Display GUI
static var openchest:Boolean=false;
var moneybox : GameObject;
var chestopenmoneyScript : chestopenmoney;
//function Start (){
//chestopenmoneyScript = moneybox.GetComponent("chestopenmoney");
//}
function OnGUI () {
if(openchest == true)
if(GUI.Button(Rect(800,700,400,175),"You found" + chestopenmoneyScript.moneyinchest + "kir!"));{
}
}
function Update () {
if(Input.GetButtonDown("Action"))
openchest = false;
}
that's the name of the gui script that the first script fires to. As it were, praise God, I figured out last night that what I needed to do was start the game with the GUI script disabled, and then simply enable it when the chest is opened. =)
Answer by Conect11 · Dec 18, 2013 at 02:05 AM
So, as I suspected the issue was that ALL instances of the GUI script were running when one chest was opened. The answer was simply to start the game with each GUI script disabled, and enable them individually when a chest is opened.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124329im_/https://answers.unity.com/themes/thub/images/avi.jpg)