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 Satamanster · Jul 11, 2012 at 08:49 PM · guienemyupdatekill

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

Comment
Add comment · Show 1
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
avatar image Satamanster · Jul 11, 2012 at 09:32 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · 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
avatar image Satamanster · Jul 12, 2012 at 01:40 AM 0
Share

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

avatar image
0

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. ;)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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