Cannot Get GUI Window to Drag

Hi everybody, I’m new with coding and I’m having a difficult time with my GUI.window. My goal is to have a window change its size relative to another variable called from another script. While this is going on, the window is also able to be dragged around the screen. I have tried and tried, but alas no luck. If anyone is able to help me with this that would be great. Here’s the code:

public class ScriptOne : MonoBehaviour {
	private Rect _InventoryWindowRectangle;
	private const int INVENTORY_WINDOW_ID = 1;
	private int ButtonSizeW = Screen.width / 16;
	private int ButtonSizeH = Screen.height / 16;
	public int _InvtRows;
	public int _invtCols;
	void OnGUI() {
		if(_DisplayInventoryWindow == false) {
			_scriptTwo = (ScriptTwo)FindObjectOfType(typeof(ScriptTwo));
			_InventoryWindowRectangle = GUI.Window(INVENTORY_WINDOW_ID, _scriptTwo.INVwinre, InventoryWindow, _scriptTwo.nameBag);
		}
	}

	public void InventoryWindow(int id) {
		_scriptTwo = (ScriptTwo)FindObjectOfType(typeof(ScriptTwo));
		_InvtRows = _scriptTwo.yBag;
		_invtCols = _scriptTwo.xBag;
		
		for(int y = 0; y < _InvtRows; y++) {
			for(int x = 0; x < _invtCols; x++) {
				GUI.Button(new Rect( (Screen.width / 256) + (x * ButtonSizeW), (Screen.height / 32) + (y * ButtonSizeH), ButtonSizeW, ButtonSizeH), (x + y *_invtCols).ToString());
			}
		}
		GUI.DragWindow();		//This used to work until I added the info from ScriptTwo
	}

}

//Second Script Function
public class ScriptTwo : MonoBehaviour {
	void CheckBag() {							//I think this is where the problem is since im calling the location
		bagType = OtherScript.bagType;			//	for the rectangle at(10,10,...,...)
	
		if(bagType == 0) {
			INVwinre = new Rect(10,10,(Screen.width / 4) + (Screen.width / 128), (Screen.height * 3) / 32);
			nameBag = "Bag1";
			yBag = 1;
			xBag = 4;
		}
		if(bagType == 1) {
			INVwinre = new Rect(10,10,(Screen.width / 4) + (Screen.width / 128), (Screen.height * 5) / 32);
			nameBag = "Bag2";
			yBag = 2;
			xBag = 4;
		}
	}
}

I have noted where i think the error is occurring in ScriptTwo, at least where the window is not moving , that is my guess but I have not figured a way around it.

The window won’t move because you are assigning the position to the wrong variable.

// Change this
_InventoryWindowRectangle = GUI.Window(INVENTORY_WINDOW_ID, _scriptTwo.INVwinre, InventoryWindow, _scriptTwo.nameBag);

// To this
_scriptTwo.INVwinre = GUI.Window(INVENTORY_WINDOW_ID, _scriptTwo.INVwinre, InventoryWindow, _scriptTwo.nameBag);

But I can see that wouldn’t help. You are still assigning literals as the positions in your CheckBag method.

// Use this line in ScriptOne.OnGUI
_InventoryWindowRectangle = GUI.Window(INVENTORY_WINDOW_ID, _InventoryWindowRectangle, InventoryWindow, _scriptTwo.nameBag);

// In ScriptTwo, replace the INVwinre with a Vector2.
Vector2 INVwinre;

// In ScriptTwo.CheckBag, you should set the INVwinre.x to the new width, and INVwinre.y to the new height.
INVwinre.x = (Screen.width / 4) + (Screen.width / 128);
INVwinre.y = (Screen.height * 3) / 32;

// In ScriptOne, whenever you call CheckBag, update the _InventoryWindowRectangle.
_scriptTwo.CheckBag();
_InventoryWindowRectangle.width = _scriptTwo.INVwinre.x;
_InventoryWindowRectangle.height = _scriptTwo.INVwinre.y;

I’m not sure why these data are split between the two classes, but with the way you are writing them, this solution should work.

Thank you! I was having a crazy time with that. I wont be able to apply these until I get home, but ill check back in once I do so. I keep tinkering with these codes I am writing to get them to work properly, but this one was one tough cookie, what you saw was the aftermath of a GUI window toggle. Sorry about that and thank you for your help. I’ll update the code soon.

It Works!!! Thanks for your help, for some reason my mind wouldn’t figure its way around that, but next time ill try to keep from assigning literals. Thank you again.

No problem. :smile: It’s always small mistakes that break things (especially when you move stuff around after you have a working system).