Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Mar 27, 2016 at 12:51 PM by Wesoly Pan Wiesio Informatyk for the following reason:

I actually solved it, nobody was interested

avatar image
0
Question by Wesoly Pan Wiesio Informatyk · Mar 27, 2016 at 10:36 AM · gui2d gameguitext

Uncommon Text behaviour

Hello, I have a little problem with my Text component. In my game i have a int that is number of killed enemies from start game. I had to use [System.NonSerialized] because it was acting like static (it wasnt..), but point is that variable killedEnemies is increasing, while Text component is still at start value, until i stop the game - then its updating to last score. I was trying almost every solution i found and its still not working. Please, help me, because it doesnt look hard but i spend last days fighting with that.

For addition i have to mention that Health variable is working fine, for both float and text variable.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
     public GameObject bullet;
 
     private float health;
     public float damage;
     private int score;
 
     [System.NonSerialized]
     private int killedEnemies;
 
     private float speedHorizontal = 0.035f;
     private float speedVertical = 0.03f;
     private Vector2 size;
 
     //CAMERA//
     private Vector3 bottomLeft;
     private Vector3 topRight;
     private Rect cameraRect;
     //CAMERA//
 
     private Text healthText;
     private Text enemiesText;
     private Text scoreText;
 
     void Awake()
     {
         score = 0;
         killedEnemies = 0;
     }
     void Start () 
     {
         damage = 20.0f;
         health = 100.0f;
 
         size = GetComponent<BoxCollider2D> ().size;
 
         bottomLeft = Camera.main.ScreenToWorldPoint(Vector3.zero);
         topRight = Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth, Camera.main.pixelHeight));
         cameraRect = new Rect(bottomLeft.x, bottomLeft.y, topRight.x - bottomLeft.x, topRight.y - bottomLeft.y);
 
         healthText = GameObject.Find ("HealthText").GetComponent<Text> ();
         enemiesText = GameObject.Find ("EnemiesText").GetComponent<Text> ();
         scoreText = GameObject.Find ("ScoreText").GetComponent<Text> ();
     }
 
     void Update()
     {
         updateTexts ();
     }
 
     void FixedUpdate () 
     {
         controlBorders ();
         checkIfDie ();
 
         if (Input.GetKey (KeyCode.W)) 
         {
             transform.Translate(new Vector3(0.0f, speedVertical, 0.0f));
         }
         if (Input.GetKey (KeyCode.S)) 
         {
             transform.Translate(new Vector3(0.0f, -speedVertical, 0.0f));
         }
         if (Input.GetKey (KeyCode.A)) 
         {
             transform.Translate(new Vector3(-speedHorizontal, 0.0f, 0.0f));
         }
         if (Input.GetKey (KeyCode.D)) 
         {
             transform.Translate(new Vector3(speedHorizontal, 0.0f, 0.0f));
         }
         if (Input.GetKeyDown (KeyCode.Space)) 
         {
             shoot ();
         }
         if (Input.GetKeyDown (KeyCode.K)) 
         {
             //enemyKilled ();
         }
     }
 
     void controlBorders()
     {
         transform.position = new Vector3(
             Mathf.Clamp(transform.position.x, cameraRect.xMin + size.x/4, cameraRect.xMax - size.x/4),
             Mathf.Clamp(transform.position.y, cameraRect.yMin + size.y/4, cameraRect.yMax - size.y/4),
             transform.position.z);
     }
 
     public void takeHealth(float hea)
     {
         if ((health + hea) > 100) 
         {
             health = 100;
         } 
         else 
         {
             health += hea;
         }
     }
 
     void shoot()
     {
         Vector3 tempPos = transform.position;
         tempPos.x += 1.0f;
         Instantiate (bullet, tempPos, Quaternion.identity);
     }
 
     public void takeHit(float dam)
     {
         health -= dam;
         Debug.Log ("Took hit " + dam);
     }
 
     public void enemyKilled()
     {
         score += 1;
         killedEnemies += 1;
         Debug.Log (killedEnemies);
     }
 
     void checkIfDie()
     {
         if (health <= 0) 
         {
             Destroy (this.gameObject);
         }
     }
     void updateTexts()
     {
         healthText.text = "Health: " + health;
         enemiesText.text = "Killed: " + killedEnemies;
         scoreText.text = "Score: " + score;
     }
 }
 
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

72 People are following this question.

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

Related Questions

TextUI text changes size if resolution changes 1 Answer

Displaying GUIText on top of Unity UI Image? 0 Answers

How can I update UI Text score using PlayerPrefs.Getint() on startup? 1 Answer

Music playing from 2 different scenes - how to fix? 1 Answer

how to add picked up items in gui? 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