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

C# Messed up get set UI :s

Discussion in 'Scripting' started by traderain, Apr 11, 2015.

  1. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    So i am creating a scipt which gives the ability to 2 gameobject to kill each other.
    I don't get but in the log they do get damage but the UI and the parameters don't update.

    Code (CSharp):
    1. player_stats ps = new player_stats();
    2.     pepe pep = new pepe();
    3.  
    4.     void OnGUI()
    5.     {
    6.         GUI.Box(new Rect(0,0,width,heihght),"Name:" + ps.name +"\n Attack:"+ ps.Player_damage + "\n Health:"+php);
    7. }
    8.  
    9. void Update()
    10.     {
    11.         php = ps.Health;
    12.         ehp = pep.Health;
    13.     }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pepe : MonoBehaviour {
    5.     public static pepe Instance;
    6.     public GameObject thisob;
    7.     public string monster_name = "Pepe";
    8.     public int pepe_damage = 6;
    9.     public int pepe_armor = 2;
    10.     public int pepe_health = 100;
    11.     public bool pepe_dead = false;
    12.  
    13.     public int Health
    14.     {
    15.         get { return pepe_health; }
    16.         set { pepe_health = value; }
    17.  
    18.     }
    19.     void Awake(){
    20.         Instance = this;
    21.     }
    22.     void Update()
    23.     {
    24.         if(pepe_health <= 0)
    25.             pepe_dead = true;
    26.  
    27.         if (pepe_dead == true)
    28.         {
    29.             Object.DestroyImmediate(thisob);
    30.         }
    31.     }
    32. }
    33.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Threading;
    6.  
    7. public class Fight : MonoBehaviour{
    8.     //public int enemy = p.Health;
    9. //    public int player = ps.Health;
    10.     public GameObject player_go;
    11.     pepe p = new pepe();
    12.     player_stats ps = new player_stats();
    13.     // Update is called once per frame
    14.     void  Update () {
    15.         StartCoroutine(Attack_Coroutine());
    16.     }
    17.     void Awake()
    18.     {
    19.         /*----- Might be solution ------
    20.         pepe p = player_go.AddComponent<pepe>();
    21.         player_stats ps = player_go.AddComponent();
    22. */
    23.     }
    24.  
    25.  
    26.  
    27.     IEnumerator Attack_Coroutine()
    28.     {
    29.     if((p.pepe_dead == false)&&(ps.dead == false))
    30.     {
    31.             Debug.Log (ps.Health);
    32.             ps.Health = ps.Health - (ps.Player_damage-(p.pepe_armor-ps.armorpen));
    33.             if(ps.Health <= 0)
    34.             {
    35.                 ps.Health = 0;
    36.             }
    37.         yield return new WaitForSeconds(1);
    38.             Debug.Log(p.Health);
    39.             p.Health = p.Health - (p.pepe_damage - ps.armor);
    40.             if(p.Health < 0)
    41.             {
    42.                 p.Health = 0;
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class player_stats : MonoBehaviour {
    5.    
    6.     public int money = 0;
    7.     public int Player_damage = 10;
    8.     public int xp = 0;
    9.     public int armor = 10;
    10.     public int health = 200;
    11.     public int potions = 0;
    12.     public int armorpen = 0;
    13.     public int level = 1;
    14.     public float xp_modifier = 1.75f;
    15.     public int boosterpacks = 0;
    16.     public string character_name = "Oachkatznal";
    17.     public bool dead = false;
    18.     public double time = 0;
    19.  
    20.     public int Health
    21.     {
    22.         get { return health; }
    23.         set { health = value; }
    24.        
    25.     }
    26.    
    27.     void Update()
    28.     {
    29.         if (dead == true)
    30.         {
    31.             Object.DestroyImmediate(gameObject);
    32.         }
    33.     }
    34. }
    35.  
    The thing is that i am trying to do is that every second (Starting with the player) they attack player,pepe,player,pepe so on. And if they get damaged represent it in the hud,

    Any help apriciated thanks in advance.
     
  2. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    It could be because you create a new instance of player_stats() and pepe(). If this is the case, you are only getting the reference from the newly created component, not the original component.
    Am I wrong? Please tell me if I am.
     
  3. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Then how can i get the original component?
     
  4. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    Use this:

    public pepe pepeOrig;
    public player_stats playerStatsOrig;

    void OnGUI(){
    GUI.Box(newRect(0,0,width,height),"Name:"+ playerStatsOrig.name+"\n Attack:"+ playerStatsOrig.Player_damage+"\n Health:"+php);
    }

    void Update(){
    php = playerStatsOrig.health;
    ehp = pepeOrig.health;
    }
     
  5. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    Did it work?
     
  6. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    It kind of works. It fixes that but i get the error:
    That's:
    Code (CSharp):
    1. GUI.Box(new Rect(0,0,width,heihght),"Name:" + playerStatsOrig.name +"\n Attack:"+ playerStatsOrig.Player_damage + "\n Health:"+playerStatsOrig.health);
    How can i fix this?
     
    Last edited: Apr 12, 2015
  7. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    I tried to change the components in every possible way in the inspector but i still can't get it to work.