• 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 MuyangYan · Mar 22, 2017 at 07:21 AM · c#fpsguitexthealth

Health not working!

Hi, I'm fairly new to Unity and I've been having a problem with a first-person-shooter game I am making. In the game, you shoot at red balls to kill them and survive as long as possible. I have made a GUItext called "Health", and have set the variable "health" to 100. I print out the health and it says 100. Good. The problem is that when you touch a red ball, it prints the health again. It's supposed to go down from 100, but this time it says the same number health was when you last played the game and its negative! It never sets to 100 in the first place! The GUIText is also not working. I am using Unity 5.4.1. This is my code for the health:

 private int Frame;
 public GameObject Enemy;
 private Vector3 spawnPosition;
 private Quaternion spawnRotation;
 public GUIText Health;
 private int enemycount;
 private int health;
 void Start () {
     enemycount = 0;
     ResetHealth();
     Vector3 textPosition = new Vector3(0, 1, 0);
     Quaternion textRotation = new Quaternion(0, 0, 0, 0);
     Health = (GUIText)Instantiate(Health, textPosition, textRotation);
     Health.text = "Health: " + health;
     StartCoroutine(wait());
     Frame = 0;
     spawnPosition = new Vector3(Random.Range(165, -165),20,Random.Range(165, -165));
     spawnRotation = new Quaternion(0, -135, 0, 0);
     Instantiate(Enemy, spawnPosition, spawnRotation);
     enemycount++;
 }
 void ResetHealth()
 {
     health = 100;
     Debug.Log(health);
 }
 IEnumerator wait()
 {
     yield return new WaitForSeconds(1);

 }
 void FixedUpdate () {
     spawnPosition = new Vector3(Random.Range(165, -165), 20, Random.Range(165, -165));
     spawnRotation = new Quaternion(0, -135, 0, 0);
     Frame++;
     if (Frame % 100 == 0 && enemycount < 20)
     {
         Instantiate(Enemy, spawnPosition, spawnRotation );
         enemycount++;
     }
 }
 void UpdateScore()
 {
     Health.text = "Health: " + health;
 }
 public void hurt()
 {
     Debug.Log("Hurtbefore" + health);
     health--;
     Debug.Log("Hurt" + health);
     UpdateScore();
     wait();
 }

Any help is greatly appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Koen-Matthijs · Mar 22, 2017 at 07:39 PM

Hi

Before helping you on your way I'm going to assume you're using GUIText because of legacy reasons. If not, I suggest you upgrade to 5.5 and use Text instead. Anyways, not important to your issue..

But first things first: something I notice in your Start() method :

 Health = (GUIText)Instantiate(Health, textPosition, textRotation);

Is there a reason why you'd instantiate an object with a copy of itself? Is this even necessary? Normally your GUI Text object should be there by reference (I mean: you made it public for a reason, probably to assign it in the inspector of the script).

Look into that first. And could you also elaborate on when exactly the "hurt()" method is called? Is that on collision with the red ball? If so, could you post that script too?

Kind regards,

Koen Matthijs

Comment
Add comment · Show 3 · 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 MuyangYan · Mar 22, 2017 at 10:45 PM 0
Share

@$$anonymous$$oen-$$anonymous$$atthijs The GUItext is a prefab and an instance of it is made at the start of the game. It is a prefab because otherwise I couldn't have assigned it to the GameController GameObject I made. Here is the code that calls hurt() in the enemy:

 public float speed;
 private Transform myTransform;
 private Transform target;
 public GameController otherScript;
 void OnTriggerEnter(Collider other)
 {
     otherScript.hurt();
 }
 void Start()
 { 
     myTransform = this.transform;
     target = GameObject.FindWithTag("Player").transform;
 }
 void FixedUpdate()
 {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), speed * Time.deltaTime);
     myTransform.position += myTransform.forward * speed * Time.deltaTime;
 }
avatar image MuyangYan · Mar 24, 2017 at 04:57 AM 0
Share

wait a sec I just realized that it was negative it was actually going down this whole time (see edited question)

avatar image MuyangYan · Mar 24, 2017 at 07:51 PM 0
Share

The GUItext is a prefab and I make an instance of it in the beginning of the game. It is a prefab because I couldn't assign it to my GameController otherwise. Also the hurt() function is called in this script for the enemy:

 public float speed;
 private Transform myTransform;
 private Transform target;
 public GameController otherScript;
 void OnTriggerEnter(Collider other)
 {
     otherScript.hurt();
 }
 void Start()
 { 
     myTransform = this.transform;
     target = GameObject.FindWithTag("Player").transform;
 }
 void FixedUpdate()
 {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), speed * Time.deltaTime);
     myTransform.position += myTransform.forward * speed * Time.deltaTime;
 }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Health and Damage [C#] 2 Answers

Can someone help me with GUI script what I have? 1 Answer

Non-static member Unity3d error 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 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