GUI button function - click to activate, click again to deactivate

Hi All,

I have a GUI button called ‘Create Waypoint’.

What I wnat to happen is when the user presses this button, it remains down (active), so when the player then clicks on the screen a waypoint point appears, then once finished the player should click ‘Create waypoint’ again to deactive the waypoint placement system.

How would one go about doing this?

presumably I also need to stick something in the update function as well to make the instantiated object appear.

	void OnGUI() {
		         
		
		if (GUI.RepeatButton(new Rect (0,0, 500, 100), "Create Waypoint")){
			
			
	
		float mousex = Input.mousePosition.x;
		float mousey = Input.mousePosition.y;
		Ray ray = camera.ScreenPointToRay (new Vector3(mousex,mousey,0));
		
		 RaycastHit hit;
		
		if (Input.GetMouseButtonDown(0)){
			
			
		     if (Physics.Raycast(ray, out hit))
            {
			  Transform waypoints = Instantiate(waypointObject, hit.point, Quaternion.identity) as Transform ;
            }
			
		}
		
	}
}

Regards,

John

var a =0;

function OnGUI() 
{ 
       if (GUI.Button(new Rect (0,0, 500, 100), "Create Waypoint"))
		{
        
		if(a == 0)
		{
		      a=1;
                }
		else 
		{
		      a=0;
                }
		       print(a);
                }
                if (Input.GetMouseButtonDown(0)  a==1)
                {
                           // if a is set to one then your code will work inside this if statement 
                           // and i am not sure abt the Raycast inside OnGUI, never tried that
                }
}

Cheers for the Swift reply Sushanta

The first section of your code works, its printing a in the console.

However its not taking the Input Button down If statement.

my current code:-

	int a = 0;

void OnGUI ()
	{
		         
		
		if (GUI.RepeatButton (new Rect (0, 400, 500, 100), "Create Waypoint")) {
	
			
			if (a == 0) {
				a = 1;
			} else {
				a = 0;
				print (a); // prints 'a' in the console. 
		
				if (Input.GetMouseButtonDown (1)  a == 1) { // This doesn't appear to be doing anything.
					
					Debug.Log ("waypointactive");
					
			
				}
			}
	
		} 
		
	
	}

Also having just put my raycast code stright in on the button press in the OnGUI it does seem to work, yay!

Regards,

John

A custom GUI element I wrote a while ago.

public static class BGUI
{
    public static bool ToggleButton(bool state, string text, params GUILayoutOption[] options)
    {
        var old = GUI.color;
        GUI.color = state ? Color.red : Color.white;
        if (GUILayout.Button(text, options))
        {
            state = !state;

        }
        GUI.color = old;
        return state;
    }
}

just use it like

private bool _isOn;

    void OnGUI()
    {
        if (BGUI.ToggleButton(_isOn, "switch"))
        {
            _isOn = true;
            Debug.Log("ON");
        }
        else
        {
            _isOn = false;
            Debug.Log("OFF");
        }
    }

c#, you’ll have to convert to javascript.
The button changes to red when on as well.
It’s a GUILayout. Obviously can be easily changed to a normal GUI element.

Bronco78th u jst messed up all the braces, so jst care fully watch what i have done, it will work i tried it my self and it worked for me.
for Raycast do something like this Because i am not sure that raycast will will inside OnGUI or not.

 var a1 =0;
 var raycastActive = 0;
 function Update() 
{ 
	var mousex : float  = Input.mousePosition.x;

        var mousey : float = Input.mousePosition.y;

        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
       
		if(raycastActive == 1)
		{
			if (Input.GetMouseButtonDown(0) )
			{

			   

				if (Physics.Raycast (ray, hit, 100)) 
				{
					
					var pos : Vector3 = hit.point;
					print(pos);

					
				}

			   

			}
		}
    
}

function OnGUI() 
{

                 

       

        if (GUI.Button(new Rect (0,0, 500, 100), "Create Waypoint"))
			{

			   
				if(a1 == 0)
				{
					a1=1;
				}
				else 
				{
					a1=0;
				}
					print(a1);
			}

		if(a1 == 1)
			{
				raycastActive = 1;
			}
			else if(a1==0)
			{
				raycastActive = 0;
			}

}

this is tested JS code and i have no errors. And dont use Repeat Button, this can be done with GUI.Button