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
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