Hey guys. This is the code i have inside my button (inside a void OnTriggerEnter)
else if(other.tag == "Dagger")
{
equipWeaponNotification = !equipWeaponNotification;
if(GUI.Button(new Rect(5, 165, 100, 50), "Equip Dagger"))
{
pickaxe.SetActive(false);
dagger.SetActive(true);
}
}
So what’s supposed to happen is. When the player walks over the trigger, if the object has the tag “Dagger”, it’ll show the button saying “Equip?”
Then below that i have the code that it should execute if the player clicks the button. Basically de-activating the current weapon and making the new one active. However when i click the button, the first weapon stays. Any ideas?
Note: this function works if i don’t include the button code (i walk into the trigger, the weapon changes).
GUI.Button only displays a button when placed in the OnGUI() loop in the code (which is deprecated and should only be used for writing Editor scripts).
However, for your peace of mind, I’ll explain. The GUI.Button() function being called will display the button on the screen, whether it’s in a conditional statement or not it’s going to display it. It’s a function, so it existing will run the function, get it? That said, unlike most of the GUI elements, the display function has a return type. If the button is being PRESSED in that exact frame, then it’ll return true. That’s why it’s used in conditional statements, so that the scripts inside of the condition will only run when the button is being pressed (exactly once when the press is released, technically (I think)).
Again, that only works in the OnGUI() loop, which is deprecated for the purposes of actually creating GUI elements outside of the Editor. I highly suggest you read up on the newer Canvas system instead.
EDIT: I think I misunderstood the question from speed-reading it. I’m in a hurry to go out biking and decided to answer before I left. That said, I think I answered the actual question eventually, which is “why is the button not working right?” => “because it’s not in the OnGUI() loop and can’t tell if it’s being pressed”. Hope that helps!
Ah fixed! I get it now. The code has to be in OnGUI and in the onTrigger the bool that shows whether or not the button will appear or not. Appreciate it! :')