- Home /
How to activate a object with GUI button? (java script)
I want activate a Object with GUI button: 1. Firt the Object is deactive 2. Player collusion a Trigger, Trigger activate a GUI button. 3. If I click GUI button, the Object is active. So GUI button activate the Object.
Please help with javascript for the GUI button.
Answer by hijinxbassist · May 31, 2012 at 08:41 PM
//Inactive Objects script
//Inactive obj has isTrigger Collider attached
public var myMenu:MyMenu;
function OnTriggerEnter(other:Collider)
{
if(other.CompareTag("Player"))
{
myMenu=other.GetComponent(MyMenu);
myMenu.showActivateButton=true;
myMenu.objectToActivate=gameObject;
}
}
function OnTriggerExit(other:Collider)
{
if(other.CompareTag("Player"))
{
myMenu.showActivateButton=false;
}
}
//MyMenu.js
public var objectToActivate:GameObject;
public var showActivateButton:boolean;
function OnGUI()
{
if(showActivateButton)
{
if(GUI.Button(myRect,"Activate Object")
{
objectToActivate.active=true;
showActivateButton=false;
}
}
}
This relies on: MyMenu.js is a component on the player, the trigger is on the inactive object, player is tagged as "Player",
@gordeszkakerek $$anonymous$$ind posting that answer you posted as a comment (helps others looking at this post). Once you have posted as comment(comment button is at the bottom of my post on the right side over there >>> "Add new comment") Copy/Paste what you said there to a new comment, and if you want, select the Check $$anonymous$$ark under the "Down Thumb" in the top right <^^< of my post to select this answer as the 'correct' answer. Helps ppl find relevant answers to their questions. Glad to help, keep coding!
Answer by gordeszkakerek · Jun 08, 2012 at 04:14 PM
I don't know why I can't use ,,Vote Up" button...BUT THANKS! I'TS WORK AND HELPED ME A LOT! THANK YOU!
Your answer
![](https://koobas.hobune.stream/wayback/20220613072201im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Activate Object with trigger and if leave then inactivate 2 Answers
2D game object trigger 1 Answer
Object Sound Trigger 1 Answer