- Home /
Display var on gui not working...
Ok guys, i have this script here wich is working fine except for the part of showing how many enemies i have killed so far... Then maybe i'm doing two things wrong but... Doing this:
#pragma strict
var firePrefab: Transform;
var deadEnemies: int = 0;
function OnTriggerEnter (other : Collider)
{
if(other.tag =="enemy")
{
Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
Destroy(other.gameObject);
deadEnemies = deadEnemies+1;
Debug.Log(deadEnemies);
}
Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
function OnGUI () { // every GUI update
GUI.Box (Rect ( 10,90,150,30), "Kills: " + deadEnemies); // create a text box with the health in there
}
...doesn't make it... First the debug log shows "1" after the first time i hit a tank and does not update from that on..., and second the GUI box only appears when i shoot a bullet and disappears after a while, plus it always shows "0"... What the ....? Sorry (2 months programmer here)... -.-'
Save me here please.. Thanks a lot... :D
I get it.. Since the variable is being initialized on every bullet event it does not update... I tough just maybe i could use another script variable to store this counting make this update the other, then i could also draw that variable on the other script gui... How can i do this?
Answer by OperationDogBird · Jul 12, 2012 at 01:28 AM
You should be counting 'deadEnemies' from the script that actually fires the bullets(or other more static script) rather than on the bullets script. Same with anything that needs to stay active, such as the OnGUI function. This way you have 1 gui function and one variable for dead enemies. You can make a public variable on this script that is of Type YourFireScript and pass it to the bullet when you fire, this way when your bullet kills an enemy you can call YourFireScript.deadEnemies++;(adds 1)
//Fire Script
public var deadEnemies:int;
function Fire()
{
var bullet=Instantiate(bulletPrefab,position,rotation);
bullet.GetComponent(Bullet).fireScript=this;
}
function OnGUI()
{
GUI.Label(Rect ( 10,90,150,30), "Kills: " + deadEnemies);
}
//Bullet Script
public var fireScript:Fire; //Script that fired the bullet
function OnTriggerEnter(other:Collider)
{
if(other.CompareTag("Enemy"))
{
fireScript.deadEnemies++;
}
}
Also this series right here
Destroy(gameObject);
Destroy(other.gameObject);
deadEnemies = deadEnemies+1;
Debug.Log(deadEnemies);
should end with the Destroy(gameObject) call, since that is the call to destroy the actual bullet that has this script attached(thus destroying this script). Sometimes its not a problem and sometimes it is, best to get in the habit of putting it at the bottom.
You are right, i did tought about that but since this assignment is for tommorrow i went trough the fastest solution given the code i already have wrote... It's hard, you know, because... I've just start learn program$$anonymous$$g 2 months ago, and this is a whole new world to me... :D Any way, thanks for your help... :D
Answer by Satamanster · Jul 12, 2012 at 01:14 AM
It works now....
This is how i did it:
I rewrote the script so it is like this now:
#pragma strict
var firePrefab: Transform;
var deadEnemies: TankState;
function Start()
{
deadEnemies = GameObject.Find("Tanque").GetComponent(TankState);
}
function OnTriggerEnter (other : Collider)
{
if(other.tag =="enemy")
{
Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
deadEnemies.counter = deadEnemies.counter+1;
Destroy(other.gameObject);
Debug.Log(deadEnemies);
}
else
{
Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Then i have another script called TankState on my tank (named Tanque) that updates it and writes it on the gui as i wanted. Here it is:
var health:float = 100; // variavel para a vida
var startHealth:float; // variavel para guardar a vida maxima
var loseHealthRate: float = 20.0; // valor de perda de vida
var counter: int = 0;
var loose: boolean = false;
function Update () {
//health = health-1; //(testes)
if (health <= 0) { //quando morrer
Die(); // executar die (no fundo do script)
}
// Debug.Log(counter); //Para efeito de testes
}
function Awake() {
startHealth = health;
}
function OnGUI () {
if(loose == true)
{
GUI.Box (Rect ((Screen.width/2)-75,100,150,55), "Game Over" +"\n\n" +"Inimigos mortos:" +counter);
}
GUI.Box (Rect ( 10,10,150,30), "Player Health: " + Mathf.Round(health)); // Vida na GUI
GUI.Box (Rect (10,70,150,30), "Kills: " +counter); // Numero de inimigos mortos
}
function OnTriggerEnter(other : Collider) { // enquanto estiver num trigger
if (other.collider.tag == "toolbox") { // Se o trigger for uma toolbox
if(health<=80)
{
health = health+20;
Destroy(other.gameObject);
}
else
{
health=100;
}
}
}
function Die() { // Quando morrer faz o seguinte:
loose = true;
yield WaitForSeconds(5);
Application.LoadLevel("mainMenu");
}
Now, since this is working can you please vote up in my answer so when other people see this question the know how to solve it? And to take me out of the "newbies with karma under 15" group. Thanks a lot. Have a nice game making. ;)
Your answer
Follow this Question
Related Questions
Script Not counting enemy kills. 1 Answer
kill script! 2 Answers
Count the Score 1 Answer
how can i destroy a cube when i press a gui button??? 1 Answer
Kill Streak Help 3 Answers