- Home /
Integer value being constantly reset to original value after subtraction
So I am having an issue with subtracting health from the enemy's objective. When the enemy AI is in range of the gate, it deals 8 damage to it. The health of the gate is 24. Naturally, in the sequence of 3 hits it would go from 24 to 16, 8 and 0. The code however will stop subtracting after the first hit, so that it will go from 24 to 16 and continue to stay on 16.
After putting Debug.Log(wallhealth) in Void Update(), you can see that the wallhealth value is being reset to the original value of 24 after a hit.
From what I know, my subtraction code should be working, but there seems to be something that is resetting the integer back to its original value. Here is the entirety of the script. If anyone can help me out, that would be highly appreciated. Thank you in advance :)
SCRIPT: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class CubeAI : MonoBehaviour
{
public GameObject thePlayer;
public GameObject theWall;
float range;
public float AIspeed;
// Health of the AI
int healthmax = 12;
int health;
bool isDead;
bool WallDead;
// Damage AI deals to the wall
int AIdamage = 8;
int wallhealthmax = 24;
int wallhealth;
void Awake()
{
health = healthmax;
wallhealth = wallhealthmax;
}
// Use this for initialization
void Start()
{
thePlayer = GameObject.FindGameObjectWithTag("Player");
theWall = GameObject.FindGameObjectWithTag("WallDefend");
}
// Update is called once per frame
void Update()
{
Debug.Log(wallhealth);
// THE AI
range = Vector3.Distance(transform.position, thePlayer.transform.position);
if(range > 1)
{
transform.LookAt(thePlayer.transform.position);
transform.Translate(Vector3.forward * AIspeed);
}
if(range < 1)
{
HitWall(AIdamage);
// instantiate explosion
Destroy(gameObject);
// switch to explode camera
// slow time
// switch to FPS camera
}
}
public void HitWall(int AIdamage)
{
wallhealth = wallhealth - AIdamage;
Debug.Log("Wall hit");
Debug.Log(wallhealth);
if (wallhealth <= 0)
{
WallDestroyed();
}
}
// MANAGING SHOT DAMAGE TO AI AND DESTROYING OBJECT WHEN NO HEALTH REMAINS
public void ShotDamage(int damage)
{
if (isDead)
{
return;
}
health -= damage;
if (health <= 0)
{
Dead();
}
}
void Dead()
{
isDead = true;
Destroy(gameObject);
}
void WallDestroyed()
{
WallDead = true;
Debug.Log("Wall destroyed");
Destroy(theWall);
// initilalise end game
}
}
EDIT: The game is wave-based, so more AI is spawned every wave. There is not only one AI in the scene, as the wave spawner spawns in more AI.
Answer by Anaxis_Studio · Sep 12, 2019 at 01:48 AM
If I understand your original post and additional comments then your issue is this:
You're setting wallhealth back to the maximum (wallhealthmax) on Awake(). So every time you spawn a new enemy it resets the wall's health.
This is something you will run into if you don't decouple data more. For example, the wall should have it's own script that manages it's health. and the enemy AI should call a function from the wall script to damage it.
Wall.GetComponent<WallScript>().Damage(8);
Legend, thank you so much! $$anonymous$$akes much more sense now. And my problem has been solved, after 3 hits the wall is destroyed. Thank you good sir!
Answer by GilbertoBitt · Sep 11, 2019 at 11:58 PM
I'm trying to understand this code part. it appears you are destroying CubeIA when in range after the first wall hit.
if(range < 1)
{
HitWall(AIdamage);
// instantiate explosion
Destroy(gameObject);
// switch to explode camera
// slow time
// switch to FPS camera
}
Yes, so when the AI is within range by <1 it will run the HitWall function as well as deleting the AI since it has "hit" or "exploded" the wall and has died.
I should have mentioned there is a wave spawner that spawns in multiple of this AI running this script. There is not only one AI in the scene. Here is the script for the wave spawner if it helps you understand the game better.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : $$anonymous$$onoBehaviour
{
public static WaveSpawner instance;
public Transform enemyPrefab;
public Transform spawnpoint;
public float timeBetweenWaves;
private float countdown = 10f;
private int waveNumber = 0;
public Text WaveCountdownText;
public bool WaveInco$$anonymous$$gTF;
int spawnPointX;
int spawnPointY;
int spawnPointZ;
private void Awake()
{
// Set instance
if (instance == null)
{
instance = this;
}
}
void Update()
{
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
WaveInco$$anonymous$$gTF = true;
countdown = timeBetweenWaves;
}
countdown -= Time.deltaTime;
WaveCountdownText.text = $$anonymous$$athf.Round(countdown).ToString();
}
IEnumerator SpawnWave()
{
waveNumber++;
Debug.Log("wave inco$$anonymous$$g");
for (int i = 0; i < waveNumber; i++)
{
SpawnEnemy();
yield return new WaitForSeconds(0.8f);
WaveInco$$anonymous$$gTF = false;
}
}
void SpawnEnemy()
{
int spawnPointX = Random.Range(-40, 40);
int spawnPointY = Random.Range(2, 10);
int spawnPointZ = Random.Range(2, 40);
Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, spawnPointZ);
Instantiate(enemyPrefab, spawnPosition, spawnpoint.rotation);
}
}