Replace GUI with Keyboard

im trying out the sample code 2D platform game from Unity webpage.
The code is as follows

function DoControlsWindow (windowID : int) {
// Make the window be draggable in the top 20 pixels.
GUI.DragWindow (Rect (0,0, System.Decimal.MaxValue, 20));

GUILayout.Label ("Select a character...");

// Let the player select the character
selected = GUILayout.Toolbar (selected, targetButtonNames);

	
// If the user has selected a new character, we'll send new SetControllable messages to turn on the other character. 
// Then we'll change who the CameraScrolling script is tracking.
if ( targets[selected] != cameraScrolling.GetTarget ()) {

	targets[selected].gameObject.SendMessage ("SetControllable", true, SendMessageOptions.DontRequireReceiver);
	cameraScrolling.GetTarget ().gameObject.SendMessage ("SetControllable", false, SendMessageOptions.DontRequireReceiver);
	cameraScrolling.SetTarget (targets[selected]);
}




// Show a different instruction label depending on what was selected above.
switch (selected) {
	case 0:
		GUILayout.Label ("Instructions:

Use the left and right arrow keys to move and space bar to jump. To run, hold down the control key.");
break;
case 1:
GUILayout.Label (“Instructions:
Use the left and right arrow keys to rotate and the up arrow to thrust.”);
break;
}
}

I am trying to covert this GUI into some keyboard shortcuts.

If i understand right, i need to write the code inside the Update function.

function Update()
{
     if(Input.GetKey(KeyCode.G) )
     {
          cameraScrolling.SetTarget (targets	[0], true);
    }
}

How to check for the current target of camera?

If you just want to replace the toolbar with keyboard shortcuts, replace this:

selected = GUILayout.Toolbar (selected, targetButtonNames);

with your shortcuts:

if (Event.current.type == EventType.KeyDown)
{
    switch(Event.current.keyCode)
    {
    case KeyCode.Alpha0:
        selected = 0;
        break;
    case KeyCode.Alpha1:
        selected = 1;
        break;
    // [...]
    }
}

Change the keys to your needs.

For more information see the Event class

Thnx for the answer. But couldnt get the code to working. Still the default target is active.

if (Event.current.type == EventType.KeyDown)
{
	switch(Event.current.keyCode)
	{
		case KeyCode.G:
    		selected = 0;
    	break;
		case KeyCode.H:
    		selected = 1;
    	break;
	}
}

	
// If the user has selected a new character, we'll send new SetControllable messages to turn on the other character. 
// Then we'll change who the CameraScrolling script is tracking.
if ( targets[selected] != cameraScrolling.GetTarget ()) {
	targets[selected].gameObject.SendMessage ("SetControllable", true, SendMessageOptions.DontRequireReceiver);
	cameraScrolling.GetTarget ().gameObject.SendMessage ("SetControllable", false, SendMessageOptions.DontRequireReceiver);
	cameraScrolling.SetTarget (targets[selected]);
}

But i was able to get the code working using Update function

function Update (){

if(Input.GetKey(KeyCode.G) )
{
	targets[0].gameObject.SendMessage ("SetControllable", true, SendMessageOptions.DontRequireReceiver);
	cameraScrolling.SetTarget (targets	[1], true);
	for ( i=0; i < targets.Length; i++) 
	targets*.gameObject.SendMessage ("SetControllable", (i == 1), SendMessageOptions.DontRequireReceiver);*
  • }*
  • else if(Input.GetKey(KeyCode.H) )*
  • {*
  •   targets[1].gameObject.SendMessage ("SetControllable", true, SendMessageOptions.DontRequireReceiver);*
    
  •   cameraScrolling.SetTarget (targets	[0], true);*
    
  •   for ( i=0; i < targets.Length; i++)* 
    

_ targets*.gameObject.SendMessage (“SetControllable”, (i == 0), SendMessageOptions.DontRequireReceiver);_
_
}_
_
}*_
Not sure if this is the perfect method of execution