- Home /
Figured it out when I read my own question. DOI
float error with screen.width/height
I'm trying to create two windows, positioned using the screen width and height variables.
Thought I had this down, but i'm getting two errors with this script, i'm not sure what it is, I think it might be the subtractions, but i've tried all sorts to see if something wasn't casting properly as a float, but it all seems to.
If I un comment the lines in the gui function, I get about 14 different errors.. I imagine that's because they rely on the variables.
This is the error, the same twice. [ Assets/Scripts/Equipment.cs(10,44): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Equipment.windowHeight']
float windowWidth = (Screen.width / 5f) * 2f;
float windowHeight = (Screen.height / 3f) * 2f;
float heightGap = (Screen.height - windowHeight) / 2f;
float widthGap = (((Screen.width / 2f) - windowWidth) / 2f);
//Rect windowOne = new Rect(widthGap, heightGap, windowWidth, windowHeight);
//Rect windowTwo = new Rect(((Screen.width / 2f) + widthGap), heightGap, windowWidth, windowHeight);
void OnGUI()
{
// GUI.Window (1, windowOne, InventoryWindow, "Inventory" );
// GUI.Window (2, windowTwo, EquipWindow, "Equip" );
}
void InventoryWindow(int windowId){
}
void EquipWindow(int windowId){
}
Closed because I answered it myself after reading the question through.
Rookie mistake, needed to initialise them in awake or something. Sorry for the wasted thread guys!
line 10 of the code you posted is blank. it's important that error line numbers match something...
where are the various window calculations? in their own function or just hanging around the mono behaviour script? the latter is likely to be causing that error. if it's stuff that you want to happen once, at the start then Awake()
or Start()
are good places.
For anyone co$$anonymous$$g in later: The float fields in this c# class can not use the instance field variables (windowWidth and windowHeight) while declaring and initializing fields. Fields are one of the first things created when a type/class is instantiated, i'm sure @Buzzdev moved the initialization of anything using a field var into start/awake.