GUI Button Re-Act To Keypress;

Solved :slight_smile: Arterie edited my code :slight_smile:

Thanks guys!!!

Something like

if(GUI.Button(new rect(0,0,200,20), "Loot it") || Input.GetButton("LootButton")){
   //Loot the chest
}

I’ve tried it and to no avail; If i put a || between the two then i’m getting a red and orange line basically a syntax error however, it builds absolutely fine.

I really need this sort or days of work have gone down the pan. Please help guys its greatly greatly appreciated!

Current Code:

	private void ScavengeWindow(int id)
	{
		if (source.Loot.Count == 0)
		{
			return;
		}
		
		Scavenge_Window_Slider = GUI.BeginScrollView(new Rect(GUI_Offset*.5f, 15, Scavenge_Window_Rect.width - 10, 70), Scavenge_Window_Slider,
		new Rect(0, 0, (source.Loot.Count * Button_Width) + GUI_Offset, Button_Height + GUI_Offset));
		
		for(int cnt = 0; cnt < source.Loot.Count; cnt ++){
		if (GUI.Button(new Rect(5 + Button_Width * cnt, GUI_Offset, Button_Width, Button_Height), source.Loot[cnt].Name))
		if (Input.GetButton("Interact Button"))
			{
				source.Loot.RemoveA(cnt);
			}
		}

You stated in your fist post that you dont mind taking all the items by clicking once.

So you could just display your lootwindow and a different function could be called once the key is pressed that loops through the loot and takes each item.

That is a very good idea;

however, when it comes to programming i’m very slow due to being a 3D artist and this is a whole new field for me; if you could perhaps help me I would GREATLY appreciate that, i could help you with 3D assets, or something

Note i dont know how your code works, so i just add ‘lootWindowOpen’ so looting is only possible if the window is open. You would need to implement that.

void Update(){
 if(Input.GetButtonDown("Loot Button")  source != null  lootWindowOpen){
  for(int cnt = 0; cnt < source.Loot.Count; cnt ++){
   source.Loot.RemoveA(cnt);
  }
 }
}

Arterie - Your amazing dude; Cheers for the changes you made :slight_smile:

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

 

public class Scavenge_GUI : MonoBehaviour {

    

    private const int Scavenge_Window_ID = 0;

    private Rect Scavenge_Window_Rect = new Rect(0,0,0,0);

    private Vector2 Scavenge_Window_Slider = Vector2.zero;

    public static Scavenge source;

    

    public float Button_Width = 40;

    public float Button_Height = 40;

    public float GUI_Offset = 10;

    public float Scavenge_Window_Height = 90;

    public bool Display_Scavenge_Window = false;

    

    void Start () 

    {

        Display_Scavenge_Window = false;

    }

    

    private void OnEnable()

    {

        Messenger.AddListener("DisplayLoot", DisplayLoot);

        Messenger.AddListener("Close Loot Source", ClearWindow );

    }

    

    private void OnDisable()

    {

        Messenger.RemoveListener("DisplayLoot", DisplayLoot);

        Messenger.RemoveListener("Close Loot Source", ClearWindow);

    }

    

    void Update () 

    {

        if (Input.GetButton("Cancel Button"))

            ClearWindow();

        

        if (Input.GetButton("Interact Button")  source != null  Display_Scavenge_Window == true)

        {

            for(int cnt = 0; cnt < source.Loot.Count; cnt ++){

  		 		source.Loot.RemoveA(cnt);

  			}

        }

        

    }

        

    void OnGUI()

    {

        if (Display_Scavenge_Window)

        Scavenge_Window_Rect = GUI.Window(Scavenge_Window_ID, new Rect(GUI_Offset, Screen.height - (GUI_Offset + Scavenge_Window_Height), 

        Screen.width - (GUI_Offset * 2), Scavenge_Window_Height), ScavengeWindow, "Scavenge");

    }

    

    private void ScavengeWindow(int id)

    {

        if (source.Loot.Count == 0)

        {

            return;

        }

        

        Scavenge_Window_Slider = GUI.BeginScrollView(new Rect(GUI_Offset*.5f, 15, Scavenge_Window_Rect.width - 10, 70), Scavenge_Window_Slider,

        new Rect(0, 0, (source.Loot.Count * Button_Width) + GUI_Offset, Button_Height + GUI_Offset));

        

        for(int cnt = 0; cnt < source.Loot.Count; cnt ++)

            GUI.Button(new Rect(5 + Button_Width * cnt, GUI_Offset, Button_Width, Button_Height), source.Loot[cnt].Name);

        

        GUI.EndScrollView();

    }

    

    private void DisplayLoot()

    {

        Display_Scavenge_Window = true;

    }

    

    private void ClearWindow()

    {

        Display_Scavenge_Window = false;

    }

}

I corrected some syntax errors and -for me- this code is now syntax error free.

Thanks, but for the little work i did here you dont need to give anything. That would be like selling a cube one made in blender :wink:

Somehow it added alot of extra empty lines inbetween when i copied your code…sorry about that :smile:

Wow;

That is AMAZING!

Thank you so much dude; I was looking through my code comparing to find i missed like 2 lines :open_mouth:

I really do appreciate the time it took ya to write that; THANKS!

:smile: :smile: :smile: +1+1!!!