Not working variable ++

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

publicclassEnemyControl : MonoBehaviour

{

publicint kills;

publicfloat speed;

publicbool isMoving;

privateint hp;

privateGameObject player;

publicGameObject AlienBulletPrefab;

// Use this for initialization

void Start()

{

player = GameObject.Find(“Soldier”);

InvokeRepeating(“CreateBullet”, 2, 3);

if (this.gameObject.tag == “Enemy1”)

{

hp = 50;

}

}

// Update is called once per frame

void OnTriggerEnter2D(Collider2D other)

{

if (other.gameObject.tag == “Bullet”)

{

hp -= 10; //kitas budas: hp -= other.gameObject.GetComponent().damage;

Destroy(other.gameObject);

if (hp == 0)

{

Destroy(this.gameObject);

kills++;

}

if (kills == 3)

{

Application.LoadLevel(1);

}

}

}

I don not understand your question, and I do not understand why you’re asking it in a poll, and I can’t read your code because of the awful formatting.

Use code tags to post your code and try to better describe your issue, preferably without using a poll. People might be more inclined to help you if you do all of that.

1 Like

Try to set kills = 0; in Start funtion

Hi, look there: http://forum.unity3d.com/threads/not-working-kills-variable-working-only-when-kills-1.411718/

still not working

So this is the script on your enemies that you are killing right?
Look here

Destroy(this.gameObject);

kills++;

That code doesnt make sense. You are storing kills on this gameobject, but then you destroy it, so kills are always going to be 0.

You are going to have to store the kills on your player, or I guess make kills static, but you should probably avoid that for this situation. Actually, static wont work either since you set kills to zero in start, so everytime you spawn an enemy, kills would reset.

1 Like

I thinking about this but when i put on random object in the scene show a lot of warnings and not working , so now i makeover script and put on player. Thank you

1 Like