how to make it so if i'm touching my door the option of clicking a button is available but if im not then the option is not available
how to make it so if i'm touching my door the option of clicking a button is available but if im not then the option is not available .... thanks in advance p.s THAT RYHMES!!!!!
Answer by Droidenkiller · Jul 11, 2016 at 01:18 AM
You could just add a script to that door and then check in the OnCollisionEnter method if the object that collides with that door is you (I guess you mean the player). Then you could for example just set a public static boolean variable to true and check for that in your OnGUI method. Or if your using the new UI system you could get the script-component of the GameObject that controls visibility and call the corresponding method to show the button. Then do pretty much the same thing in the OnCollisionExit method of the script of the door, just that you make the button disappear now.
Those pages might help you: - https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html - https://docs.unity3d.com/ScriptReference/Collider.OnCollisionExit.html - https://docs.unity3d.com/ScriptReference/Collision.html
Here's a small example script (I didn't test it and you probably have to adjust it to your needs):
GameObject GUIManager;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "player") //Assuming the tag of your player is player
{
//If you want to use a static variable use e.g. "StaticVars.buttonVisible = true;" and declare buttonVisible as a public static variable in the script named StaticVars or:
GUIManager.GetComponent<GUIManagerScript>().makeButtonAppear();
}
}
void OnCollisionExit(Collision collision)
{
GUIManager.GetComponent<GUIManagerScript>().makeButtonDisappear();
//Or check if it was the player if you don't want to execute it everytime something stops colliding with your door
}