Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 04, 2014 at 02:43 PM by RyanPaterson for the following reason:

Figured it out when I read my own question. DOI

avatar image
0
Question by RyanPaterson · Aug 04, 2014 at 02:36 PM · gui

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){
             }
     
 

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanPaterson · Aug 04, 2014 at 02:44 PM 1
Share

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!

avatar image gjf · Aug 04, 2014 at 02:47 PM 0
Share

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.

avatar image Landern · Aug 04, 2014 at 02:52 PM 0
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

how to "blink" more often? 2 Answers

Erroe when resizing GUITextures in C# file 1 Answer

how to call a gameobject in one function(mouse selection) to another function (GUI) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges