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 /
avatar image
0
Question by sinkler747 · Jun 16, 2013 at 08:51 PM · guierrorprogrammingscreen

problem with GUI errors and screen resolution

There are 2 problems i am having. 1. when i use my GUIText and set the resolution to screen.width and the screen.height, when i change the screen size bigger or smaller it does not adjust with the play area. 2. when i created the GUI's it gives me several errors(the red exclamation marks), but i am still able to play. below is the script and screen shots of the errors and the guiText. thanks for any assistance. this is in Jscript

sorry i tried to upload the screen shots but it did not work. the errors i was having said: UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. FlashLight..ctor () (at Assets/myScripts/FlashLight.js:74)

and.. get_guiText can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

then...

ArgumentException: get_guiText can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. FlashLight..ctor () (at Assets/myScripts/FlashLight.js:74)

here is the script

 /*
 for 1st person shooter
 create an empty gameObject, name it FlashlightHolder and place it on the camera.
 inside the newly created empty Gameobject add a spot light and name it "flashlight" rotate as needed because it ussually is face down
 
 create a tag and name it " HeadLamp " make sure you follow the spelling and capitalizations.
 assign this script to the player.
 
 then create 3 GUI Text, found under  GameObject 
                                          create other 
                                                  GUI Text
                                                  
 name them:
     GUI Text_batteryLife
     GUI Text_batteryLifeRemaining_tex
     GUI Text_flashlight_statement
     
     all should be default positioned at:
         x 0.5
         y 0.5
         z 0
     
 in the "GUI Text_batteryLife" change the following:
         Text = 400
         
         Pixel offset 
         x = 0
         y = 0
         
         
         font size = 20
         
         all others can remain the same or change them how u see fit.
         
         
         
 in the " GUI Text_flashlight_statement " change the following:
 
         Text = TO Turn Flashlight On or Off, Press "O"
         
         Pixel offset 
         x = 0
         y =  0
         
         font size = 20
         
         
 in the " GUI Text_batteryLifeRemaining_tex " change the following
         Text = Battery Life Remaining
         
         Pixel offset
         x = 0
         y = 0
         
         font size = 20
 
 
 
 */
 
 
 
 
 #pragma strict
 var batteries: float = 2.0f;
 var batteryLife: float = 420.0f;
 var batteryReductionSpeed: float = 1.0f;
 
 var lightRange: float = 100.0f;
 
 var horizontalOffset01: float = 0.29f;
 var verticalOffset01: float = 0.47f;
 
 var horizontalOffset02: float = 0.37f;
 var verticalOffset02: float = 0.4f;
 
 var guiLifeOfBattery = guiText;
 var guiRemainingBatLife = guiText;
 var guiInstruction = guiText;
 
 
 
 
 function Awake () 
 {
     GameObject.FindWithTag("HeadLamp").light.enabled = false;
     batteries--;
     GameObject.Find("GUI Text_batteryLife").guiText.text = batteryLife.ToString();
     
     guiLifeOfBattery.enabled = false;
     guiRemainingBatLife.enabled = false;
     guiInstruction.enabled = true;
     
 //    guiLifeOfBattery.pixelOffset = Vector2(Screen.width * horizontalOffset02, Screen.height * verticalOffset02);
 //    guiRemainingBatLife.pixelOffset = Vector2(Screen.width * horizontalOffset01, Screen.height * verticalOffset01);
 }
 
 function Update () 
 {    
     if(GameObject.FindWithTag("HeadLamp").light.enabled == true)
     {
         guiLifeOfBattery.enabled = true;
         guiRemainingBatLife.enabled = true;
         guiInstruction.enabled = false;
     
         batteryLife = batteryLife -(batteryReductionSpeed * Time.deltaTime);
         GameObject.Find("GUI Text_batteryLife").guiText.text = batteryLife.ToString("#.");
         
         guiLifeOfBattery.pixelOffset = Vector2(Screen.width * horizontalOffset02, Screen.height * verticalOffset02);
         guiRemainingBatLife.pixelOffset = Vector2(Screen.width * horizontalOffset01, Screen.height * verticalOffset01);
         
     }
         if(Input.GetKeyDown("o") && !GameObject.FindWithTag("HeadLamp").light.enabled == true && batteries >= 0) 
         {
             if(batteryLife <= 0 && batteries > 0)
             {
                 batteries--;
                 batteryLife = 420;
                 lightRange = 100;
                 GameObject.FindWithTag("HeadLamp").light.range = lightRange;
                 
             }
             GameObject.FindWithTag("HeadLamp").light.enabled = true;
             
         }
         
         
         else if(Input.GetKeyDown("o") && GameObject.FindWithTag("HeadLamp").light.enabled == true || batteryLife == 0)
     {
         
         GameObject.FindWithTag("HeadLamp").light.enabled = false;
         guiInstruction.enabled = true;
     }
     if(batteryLife <= 0)
         {
             batteryLife = 0;
             GameObject.FindWithTag("HeadLamp").light.enabled = false;
             guiInstruction.enabled = true;
         }
         if(batteryLife <= 50)
         {
 
             GameObject.FindWithTag("HeadLamp").light.range = lightRange * batteryLife / 20;
             
             
                 if(batteryLife <= 25)
                 {
 
                     GameObject.FindWithTag("HeadLamp").light.range = lightRange * batteryLife / 20;
             
                 
                 }
         }
     
         
     
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Skalde · Jun 16, 2013 at 09:18 PM

try to script the gui text in a OnGUI function, that might solve the problem..

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Multiple Cars not working 1 Answer

Why is networkbehaviour underlined in green? 1 Answer

3 Days and I still can't figure out c# reflection, please help? Trying to use a protected Type and method. 1 Answer

HighScore analytics 0 Answers

rigidbody AddForce(transform.left) error 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