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 /
avatar image
0
Question by lachlan_orton · Sep 11, 2019 at 09:57 PM · scripting problemintegerhealth-deduction

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.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
Comment
Add comment · Show 1 · 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 lachlan_orton · Sep 12, 2019 at 05:57 AM 0
Share

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!

avatar image
0

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
  
          }

Comment
Add comment · Show 1 · 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 lachlan_orton · Sep 12, 2019 at 01:19 AM 0
Share

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);
     }
 
 
 }
 

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.

Follow this Question

Answers Answers and Comments

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

Best way to set up a city trading system script?,Best way to script city trading system? 1 Answer

Instantiating a prefab and values 1 Answer

How can I make the Random.Range in this spawning script a PUBLIC adjustable variable? The (0, 35) code limit gives errors when I need the prefab list to fluctuate. An example code would help. Thanks! 1 Answer

How to make Boolean not add more than one integer? 2 Answers

Score with Text going up by random increments 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