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:48 PM · guienemybulletkill

Script Not counting enemy kills.

I'm sorry if this question appears as double post, but i think my other question was not approved from the queue for some reason. Anyway, can you please help me here guys?

I have this script attached to my cannon:

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); deadEnemies = deadEnemies+1; Destroy(other.gameObject); Debug.Log(deadEnemies); } else { 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 }

The script is then attached to my bullet prefab, and for the part of hiting the enemies and destroying them... It works. Now the two problems i'm facing are the fact that, first, the variable deadEnemies only updates once, at the first enemy i destroy, and the other problem is that i can't manage to make the count appear on the gui box, it was supposed to show the variable deadEnemies at the top, and it only appears for a few seconds, everytime i press the fire button. And it is not updated since always shows zero, in other words, it's not even giving me the same value as debug log. What am i doing wrong.. can you help me please? I have to deliever this assigment tommorow and this isn't working as i wanted to. Save me please. Thanks...

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 ScroodgeM · Jul 22, 2012 at 08:10 AM 0
Share

format your code with 'pre' tag (101010 button)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Doireth · Jul 22, 2012 at 08:29 AM

If the script is attached to the bullet prefab then the integer holding the dead enemies value is destroyed with the bullet. Attach the script to the object firing the bullet instead (i.e, the player) as it won't be destroyed.

Or you could make the deadenemies value static

 static var deadEnemies : int  = 0;

that way it would be global to all instances of the script and should remain (I think).

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display var on gui not working... 2 Answers

how can i destroy a cube when i press a gui button??? 1 Answer

Randomizate bullets help 2 Answers

kill script! 2 Answers

Count the Score 1 Answer


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