- Home /
How to index objects
Hi guys, im not sure if this is the correct terminology. But what I am trying to do is.. I have a health bar connected to a tower. When a unit is near it decreases. Works great. But when I have multiple of the same tower on the screen its buggy. I think its because I am using the same variable for both towers. How do I make it so their variables are not shared? I have my code below with comments. It is applied to the "towers" in my game.
using UnityEngine;
using System.Collections;
public class Tower : MonoBehaviour
{
private int testInt=0; // Update is called once per frame
public Texture2D selectionHighLight = null;
private Vector3 captureBarPos = Vector3.zero;
private static float captureBarLength = 500f;
private static float maxCaptureBarLength =500f;
private static float length = 50f;
public bool playerCapture =false;
void Update(){
captureBarPos = Camera.main.WorldToScreenPoint (transform.position);
print("target is " + captureBarPos.x + " pixels from the left");
if(captureBarLength>=0){ //this is the code that captures faster based on amt of units in area
switch(testInt){
case 1: captureBarLength-=1;
break;
case 2: captureBarLength-=2;
break;
case 3: captureBarLength-=3;
break;
}
//test
if(captureBarLength<3){//just a test to see color change
playerCapture=true;
}else{
playerCapture=false;
}
}
if (captureBarLength <= maxCaptureBarLength && testInt == 0) { //refill bar when no units around
captureBarLength+=5;
}
if (playerCapture == true) {
renderer.material.color = Color.red; //change color when captured
} else {
renderer.material.color = Color.white;
}
}
void OnTriggerEnter(Collider col) { //detecting if "unit" is in the area
if(col.collider.gameObject.name=="Unit"){
testInt+=1;
Debug.Log(testInt);
}
}
void OnTriggerExit(Collider col) { //detecting if "unit" is in the area
if(col.collider.gameObject.name=="Unit"){
testInt-=1;
Debug.Log(testInt);
}
}
private void OnGUI(){
GUI.DrawTexture(new Rect (captureBarPos.x-30,(Screen.height-captureBarPos.y)-50,length*(captureBarLength/maxCaptureBarLength),5),selectionHighLight);
}
}
I don't see a healthBar in that code but I see static variables at the top which coupled w/ the problem description sounds like the culprit.
Answer by jjmontes · Apr 25, 2014 at 04:52 AM
Seems like your variable captureBarLength
belongs to the Tower instance, and hence shall not be static. Remember that static variables are shared among all instances of a class (in this case Tower
).
Not sure about your other static vars, double check whether they are global, or remove the static
modifier from all to be on the safe side.
@jjmontes Ill try making it not static and see what happens. I made it static because im trying to make my health bar based on percentages. I want to keep the same size health bar, no matter how much the health value is. This was posing a problem when I tried to run the game. Without static int's the bar would be like off the screen in length. Do you know how I could do this? Thanks,
length*(captureBarLength/maxCaptureBarLength)
length is the bar length, captureBarLength is what decreases, maxCaptureBarLength is how much health would need to be decreased for capture
Ok that you use a static field for maxCaptureBarLength
. But you are using length*(captureBarLength/maxCaptureBarLength)
for the Rect width, and the three variables are static. I don't fully grasp what you are trying to achieve, but doesn't look right: your bar width calculation must refer to some local field (non-static) which contains the value health. Ultimately you want to normalize that health to a float between 0 and 1 in order to multiply your maxCaptureBarLength.
Out of intuition, in your case I'd normally use something like healthNormalized * maxCaptureBarLength
, and health must not be static (after all it has a different value for each tower).
Your answer
Follow this Question
Related Questions
RTS Movement and grid questions 0 Answers
How to add gameobject inside of an array? 0 Answers
Get First Object In List 2 Answers
RTS Unit Creation Bar Queue System 1 Answer
rts developement 0 Answers