Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Damage button.

Discussion in 'Scripting' started by MrZeker, Feb 17, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, im trying to make a button that when i press it reduces the energy of the enemy in 10. its for betatesting, but i cant figure what's wrong in the code.

    when i press the button the jump animation plays (which means that the button somewhat works), but there is no change in enemy energy.

    This is the script in the enemy.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Assertions;
    7.  
    8. public class EnemyScript : MonoBehaviour
    9. {
    10.  
    11.  
    12.     [SerializeField] int EstartingEnergy = 50;
    13.     [SerializeField] int EcurrentEnergy;
    14.     [SerializeField] Slider EenergySlider;
    15.  
    16.     public int ECurrentEnergy
    17.     {
    18.         get { return EcurrentEnergy; }
    19.         set
    20.         {
    21.             if (value < 0)
    22.                 EcurrentEnergy = 0;
    23.             else
    24.                 EcurrentEnergy = value;
    25.         }
    26.     }
    27.  
    28.     public Slider EEnergySlider
    29.     {
    30.         get { return EenergySlider; }
    31.     }
    32.  
    33.     void Awake()
    34.     {
    35.  
    36.         Assert.IsNotNull(EenergySlider);
    37.     }
    38.  
    39.     // Use this for initialization
    40.     void Start()
    41.     {
    42.  
    43.         anim = GetComponent<Animator>();
    44.         EcurrentEnergy = EstartingEnergy;
    45.      }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.  
    51.         timer += Time.deltaTime;
    52.  
    53.         EenergySlider.value = EcurrentEnergy;
    54.  
    This is the script of the button.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Button1 : MonoBehaviour {
    7.  
    8.     public GameObject button;
    9.     public GameObject player;
    10.     public GameObject enemy;
    11.     private Animator playerAnim;
    12.     private Animator enemyAnim;
    13.     private int EnemyEnergy;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         enemyAnim = enemy.GetComponentInChildren<Animator>();
    19.         playerAnim = player.GetComponentInChildren<Animator>();
    20.         EnemyEnergy = enemy.GetComponent<EnemyScript>().ECurrentEnergy;
    21.  
    22.  
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.        
    28.     }
    29.  
    30.     public void ClickTheButton ()
    31.     {
    32.        playerAnim.Play("Jump");
    33.         enemyAnim.Play("Jump");
    34.     }
    35.  
    36.     public void DamageEnemy ()
    37.     {
    38.         EnemyEnergy -= 10;
    39.         enemyAnim.Play("Jump");
    40.     }
    41.  
    42. }
    43.  
    44.  
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You're just changeing a local variable that exists only in the button script.

    when you say "int = other int" it gets it by value, not by reference like the animator.
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    im a bit confused about how to use the ref.
    im trying right now with public static int . however, i cant seem to be able to put a maximum value, so the energy ends up being above 100 or below 0. :\