Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I change an int value on a script attached to a gameobject with a button?

Discussion in 'Scripting' started by Dustyman567, Apr 7, 2015.

  1. Dustyman567

    Dustyman567

    Joined:
    Oct 30, 2014
    Posts:
    5
    I am fairly new to unity and coding. In this code I am trying to make a code that subtracts hp from the enemy gameobject. Before whenever I clicked the button, instead of lowering the int of hp on the gameobject "Enemy", it lowered the int on the actual script which appeared to not affect the gameobject at all as it retained the same int value on it no matter how many times I pressed the button. I have been trying to fix it by getting the component directly from the gameobject, but I always get an error no matter how I try to do that. Here is the code for a button and hp I have been tinkering with:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ehp : MonoBehaviour {
    5.     public GameObject enemy;
    6.     public int ehp = 5;
    7.     private Ehp ehhp;
    8.    
    9.     void Awake () {
    10.         enemy = GameObject.Find ("Enemy");
    11.         ehhp = enemy.GetComponent<Ehp> ();
    12.    
    13.     }
    14.  
    15.     public void DamageButton (int damage) {
    16.                 enemy.ehp -= (damage);
    17.         Turns.currentState = Turns.BattleStates.EnemyChoice;
    18.                        
    19.                 }
    20.  
    21.  
    22.     void Update () {
    23.     if (ehp <= 0) {
    24.             Application.LoadLevel("Lose Screen");
    25.     }
    26.    
    27.    
    28. }
    29.  
    30. }      
    I have an error on enemy.ehp -= (damage); I get the error:
    error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `ehp' and no extension method `ehp' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?). Can someone help me with this please?
     
  2. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    How is the enemy GameObject set up? It needs to contain a script that keeps track of its HP. Then, you would refer directly to that script to perform any arithmetic on the hp. My best guess is that you have an enemy GameObject that doesn't have any scripts of its own. When you make the call to GameObject.Find("Enemy"), it only returns a generic GameObject, which has no real logic of its own. enemy.ehp then fails because GameObjects don't have an ehp field.

    Here's probably more what you're looking for:
    Code (CSharp):
    1. public class EnemyHealth : MonoBehaviour
    2. {
    3.   public int ehp = 5;
    4. }
    5.  
    6. public class ButtonInteraction : MonoBehaviour
    7. {
    8.   public EnemyHealth _enemyHealth;
    9.   public void DamageButton(int damage)
    10.   {
    11.     _enemyHealth.ehp -= damage;
    12.   }
    13. }
    You'll then want to make sure you put the EnemyHealth script on your Enemy GameObject, and then drag it over as a reference to the ButtonInteraction script on some other GameObject (probably the button which triggers the DamageButton function). That is generally the more "Unity" way of linking objects together than GameObject.Find
     
  3. Dustyman567

    Dustyman567

    Joined:
    Oct 30, 2014
    Posts:
    5
    New problem, it only takes away HP from the object after I stop playing. If I press the button, no immediate effect takes place. As soon as I stop playing, the health on the gameobject will drop. This means that in the actual game, no effect would be taking place. Also the int on the object will change forever and I have to reset it back to the original 5. I know I could just fix this by doing: void Start() { ehp = 5, though. The main problem is it not updating the int until after I stop playing. How can I fix this?