There are 2 problems i am having.
- 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.
- 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;
}
}
}