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 DevelopingGamesStudios · Sep 06, 2017 at 02:02 AM · variableupdate

Variable updates everywhere but Update function.

I have a variable "health" this is effected when damaged. When i debug.log the variable in the update function, it does not change, it still says the health = 100, but when i debug.log health in any other function, the variable is updated, but still stays the same in update.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 public class enemyController : MonoBehaviour {
 
 
     private Vector3 origin;
     private GameObject player;
     
 
     private NavMeshAgent nMA;
 
     public enemyType enemyType;
     
     private bool isDead = false;
     private bool attackingState;
     private bool isAttacking;
     private float maxHealth;
     private float health;
     private int level;
     private float attackDMG;
     private Animator enemyAnim;
     private bool patrollingState;
     private bool isPatrolling;
 
     // Use this for initialization
     void Awake()
     {
        
 
     }
 
     void Start () {
         this.origin = this.gameObject.transform.position;
         nMA = this.gameObject.GetComponent<NavMeshAgent>();
         isAttacking = false;
         isPatrolling = false;
         attackDMG = enemyType.baseDamage;
         player = GameObject.FindGameObjectWithTag("Player");
         enemyAnim = this.gameObject.GetComponent<Animator>();
         level = enemyType.baseLevel;
         maxHealth = enemyType.maxHealth;
         health = 100;
     }
 
     public void switckIsAttacking()
     {
         if (isAttacking)
         {
             isAttacking = false;
 
         } else 
         {
             isAttacking = true;
         }
     }
 
     public float getHealth()
     {
         return health;
     }
 
     public float getMaxHealth ()
     {
         return maxHealth;
     }
 
     public float damageHealth(float dmg)
     {
         Debug.Log("damageHealth: " + health);
         if (!isDead) {
             health = health - dmg;
         }
         
         return health;
     }
     
     public void Die()
     {
         nMA.isStopped = true;
         enemyAnim.SetBool("Dead", true);
     }
 
     public float getDamage()
     {
         return attackDMG;
     }
 
     public void Attacking()
     {
         
        if (!isDead)
         {
             
             if (this.gameObject.tag == "cQBEnemy" && Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) <= 3)
             { 
                 nMA.isStopped = true;
                 this.transform.LookAt(player.transform);
                 enemyAnim.SetBool("Attacking", true);
             } else
             {
                 nMA.isStopped = false;
                 nMA.SetDestination(player.transform.position);
                 enemyAnim.SetBool("Attacking", false);
                
 
             }
 
         }     
         
     }
 
     public void patrolling()
     {
         StartCoroutine("patrollingWaitTime");
     }
 
     IEnumerator patrollingWaitTime()
     {
         isPatrolling = true;
         nMA.SetDestination(new Vector3(Random.Range(origin.x-3, origin.x+3), 0, Random.Range(origin.z-3, origin.z+3)));
         yield return new WaitForSeconds(Random.Range(5, 10));
         patrolling();  
     }
 
     public void Kill()
     {
         Destroy(gameObject);
     }
 
     public void checkStates()
     {
         if ( Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) <= 20)
         {
             isPatrolling = false;
             patrollingState = false;
             attackingState = true;
         } else
         {
             if (Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) >= 20)
             {
                 attackingState = false;
                 if (Vector3.Distance(this.gameObject.transform.position, this.origin) >= .5f)
                 {
                     if (!isPatrolling)
                     {
                         nMA.SetDestination(origin);
                         patrollingState = false;
                     }
                     
                 } else
                 {
                     if (Vector3.Distance(this.gameObject.transform.position, this.origin) <= .5f)
                     {
                         if (!patrollingState)
                         {
                             
                             patrollingState = true;
                             patrolling();
                         }
                     }
                 }
                 
 
                 
             }
             
             
         }
     }
 
     public void checkHealth()
     {
         if (health <= 0)
         {
             Debug.Log(this.gameObject.name.ToString() + " " + health);
             health = 0;
             isDead = true;
             Die();
         }
     }
     
     
     // Update is called once per frame
     void Update () {
         Debug.Log("Update: " + health);
         checkStates();
         Vector3 vel = nMA.velocity;
         float speed = vel.magnitude; 
         enemyAnim.SetFloat("speedPercent", speed * 10);
 
        
 
         if (attackingState)
         {
             isPatrolling = false;
             patrollingState = false;
             Attacking();
         }
 
         checkHealth();
 
         
 
         
         
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlaSh-G · Sep 06, 2017 at 09:55 AM

How does your script even supposedly compile with this line??

 public enemyType enemyType;

Class names start with an uppercase letter because of things like this.

That aside, once I fixed this error, I ran the script and it worked just fine.

Console output

My guesses for your problem are:

  1. You have multiple enemies in your scene. You damage one, but then monitor the health of the other(s).

  2. You have another component (probably a script) that interferes with your health variable every update

  3. You are reading your Console wrong. Dunno how though :)

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 DevelopingGamesStudios · Sep 07, 2017 at 09:43 PM 0
Share

I figured out the unity was actually just being buggy haha. I copied the code, then deleted the script, then remade the script and pasted the code and everything worked out fine.

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

71 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

Related Questions

How to change the rate of decrease in a variable in the update function 1 Answer

Why a public varibale is not accsible inside Update() ? 1 Answer

Access a variable from another script in update function 1 Answer

coroutines : trouble editing and accessing the same public variable 4 Answers

Variable goes very high in update method 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