I’m creating a HUD element for what will eventually be a FPS game in Unity 4. This element is supposed to show a bar on the left side of the screen increase as batteries are collected in an environment. I have the following public variables declared:
public var charge0tex : Texture2D;
public var charge1tex : Texture2D;
public var charge2tex : Texture2D;
public var charge3tex : Texture2D;
public var charge4tex : Texture2D;
I set up if checks to render the proper textures on the screen as batteries are collected:
(example):
// if the variable charge is equal to 1
if(charge == 1)
{
guiTexture.texture = charge1tex;
guiTexture.enabled = true;
}
I did the same code above for 3 more batteries (with else if included to differentiate between them). At the end of my script I wrote this to allow for the pick up of the fifth battery:
I’m getting errors with the code for the fifth battery.
I’m not sure why I’m getting the errors as the rest of the batteries are working. I tried changing the code in Line 77 to what it lists and I get more errors. I’ve also never seen the second error before. What am I doing wrong? Any help would be greatly appreciated. (I know I’m new here and this is my first post. I’m going crazy trying to fix these errors so I can continue my work! :()
Raptorちゃん Can you please provide the full code so I can debug it for you? It appears to be that line from what you posted, however, if that didn’t fix it, then it most likely is something in another part of this script of yours.
////////////////////////////////////////////////////////////////////////////////////////
// Purpose: To create a HUD element that shows to the player how many batteries
// they have collected from the area.
//
//
////////////////////////////////////////////////////////////////////////////////////////
// Global variable accessible by other scripts to store charges available
#pragma strict
static var charge : int = 0;
// 5 variables to store the textures
public var charge0tex : Texture2D;
public var charge1tex : Texture2D;
public var charge2tex : Texture2D;
public var charge3tex : Texture2D;
public var charge4tex : Texture2D;
// Setup defaults of the GUI Texture component
function Start()
{
// This is enabled to make sure that the battery is not visible when game starts
guiTexture.enabled = false;
charge = 0;
}
function Update ()
{
// Series of if checks in order to render the proper textures to the screen when you are collecting
// batteries
// if the variable charge is equal to 1
if(charge == 1)
{
// Sets the texture slot of the guiTexture component to use the image file assigned to the charge1tex variable
guiTexture.texture = charge1tex;
// Turn on the guiTexture by setting its enabled value to true
guiTexture.enabled = true;
}
// else if the variable charge is equal to 2
else if(charge == 2)
{
// Sets the texture slot of guiTexturecomponent to use the image file assigned to charge2tex variable
guiTexture.texture = charge2tex;
// Turn on the guiTexture
guiTexture.enabled = true;
}
// else if the variable charge is equal to 3
else if(charge == 3)
{
// Sets the texture slot of guiTexturecomponent to use the image file assigned to charge3tex variable
guiTexture.texture = charge3tex;
// Turn on the guiTexture
guiTexture.enabled = true;
}
// else if the variable charge is equal to 4
else if(charge == 4)
{
// Sets the texture slot of guiTexturecomponent to use the image file assigned to charge4tex variable
guiTexture.texture = charge4tex;
// Turn on the guiTexture
guiTexture.enabled = true;
}
// else
// Sets the texture slot of guiTexturecomponent to use the image file assigned to charge0tex variable
else(charge == 5);
{
guiTexture.texture = charge0tex;
guiTexture.enabled = true;
}
This is the code I have. Like I said, all other variables work except for the last one (for the fifth battery).
If I may make one suggestion, it would save you adding more charge level code:
////////////////////////////////////////////////////////////////////////////////////////
// Purpose: To create a HUD element that shows to the player how many batteries
// they have collected from the area.
//
//
////////////////////////////////////////////////////////////////////////////////////////
// Global variable accessible by other scripts to store charges available
#pragma strict
static var charge : int = 0;
// 1 array to store the textures
public var chargeTexArr : Texture2D[];
// Setup defaults of the GUI Texture component
function Start()
{
// This is enabled to make sure that the battery is not visible when game starts
guiTexture.enabled = false;
charge = 0;
}
function Update ()
{
// One check in order to render the proper textures to the screen when you are collecting
// batteries
//since charge is 1 based and the array is 0 based, different checking than normal 0 based checks here
if(charge > 0 charge <= chargeTexArr.length)
{
guiTexture.texture = chargeTexArr[(charge - 1)];
guiTexture.enabled = true;
}
}