- Home /
How to activate a button?
Hi there,
I'm having a problem with my script.. It's written in javascript and I can't figure out what's wrong. I want to have a GUI Button and if I click on it I should be able to place a object in the scene. The instantiating part works fine, with a raycast. But if I add a GUI Button, it instantiates at the moment I click, and only if I click the button, so you can only place object in the button part of the screen..
Is there any way to script it so that you can instantiate if you clicked the button once, and if you click it again, you can't?
Answer by roojerry · Mar 19, 2013 at 04:27 PM
bool clickedOnce = false; //global variable for that script
//if GUI button clicked
if(!clickedOnce){
//Instantiate object
clickedOnce = true;
}
So this is what I have right now, but it still only instantiates on the button, if I want it outside the button, it does nothing.
pragma strict
var buildingThatWillBeBuild : GameObject;
function OnGUI() {
var clickedOnce : boolean = false;
if(!clickedOnce){
if (GUI.Button(Rect(10,70,50,30),"Build")) {
var ray : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, 15));
Debug.DrawRay (transform.position, ray, Color.green);
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
var rayRay = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (rayRay, hit)) {
if(hit.collider.gameObject.tag =="Ground")
{
Instantiate (buildingThatWillBeBuild, hit.point, buildingThatWillBeBuild.transform.rotation);
clickedOnce = true;
}
}
}
}
}
}
Are you saying that you want to click the button, then click again to place the building where you want it?
var buildingThatWillBeBuild : GameObject;
var clickedOnce : boolean = false;
var canBuild : boolean = false;
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),"Build")) {
if(!clickedOnce){
canBuild =true;
}
}
}
function Update(){
var ray : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, 15));
Debug.DrawRay (transform.position, ray, Color.green);
if(canBuild && Input.Get$$anonymous$$ouseButtonUp(0))
{
var rayRay = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (rayRay, hit)) {
if(hit.collider.gameObject.tag =="Ground")
{
Instantiate (buildingThatWillBeBuild, hit.point, buildingThatWillBeBuild.transform.rotation);
clickedOnce = true;
canBuild = false;
}
}
}
}
I wrote this pretty quickly, but it should be close to what you want
I get your way of thinking, but it does exactly the same as my script. It only creates a object in the button part of the screen
Your answer
Follow this Question
Related Questions
A way to instantiate an item with 90 Degree increments? 2 Answers
Changing variable in a script of a game object hit with raycast 1 Answer
[SOLVED] Only instantiating once 1 Answer
[Logical Error] Select a game object and perform actions using GUI.Button 1 Answer
Setting Scroll View Width GUILayout 1 Answer