Search Unity

Need help: Using same UI components to display different values (scriptableobjects!)

Discussion in 'UGUI & TextMesh Pro' started by JShrew, Jul 14, 2019.

  1. JShrew

    JShrew

    Joined:
    Jun 22, 2019
    Posts:
    3
    Hey guys, this is my first time posting and I have learned a lot through looking through other posts, however I can not seem to find an answer for this problem.

    So in the prototype I am working on I want to be able to interact with different objects on the overworld, and if those objects have stats I want to be able to access a menu that displays those stats. Everything is working fine, except for one thing: whenever I interact and open the "stat menu" for one of these gameObjects, it will only display the stats for one of them. If I try to interact with the other gameObject, it will display the same stats as the other gameObject.

    If I disable the first gameObject with stats, I am able to display the stats of the other gameObject, however if I enable it again it seems to override the values whenever I try to access either stats.

    The main question is:
    "Am I able to use the same UI and have it change depending on what scriptableobject it is taking the information from"



    This is the script that handles the UI

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class StatDisplayManager : MonoBehaviour
    8. {
    9.  
    10.  
    11.    
    12.     public MonsterSkeleton monster;
    13.  
    14.     public TMP_Text Name;
    15.     public TMP_Text Health;
    16.     public TMP_Text Strength;
    17.     public TMP_Text Intelligence;
    18.     public TMP_Text Defense;
    19.     public TMP_Text Skill;
    20.     public TMP_Text Speed;
    21.  
    22.  
    23.  
    24.  
    25.     void Start()
    26.     {      
    27.  
    28.         Name.text = monster.name;
    29.         Health.text = "HP: " + monster.HP.ToString();
    30.         Strength.text = "STR: " + monster.STR.ToString();
    31.         Intelligence.text = "INT: " + monster.INT.ToString();
    32.         Defense.text = "DEF: " + monster.DEF.ToString();
    33.         Skill.text = "SKL: " + monster.SKL.ToString();
    34.         Speed.text = "SPD: " + monster.SPD.ToString();
    35.  
    36.     }
    37.  
    38. }

    And this is the scriptableObject script that the first script is deriving from.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8.  
    9.  
    10. [CreateAssetMenu(fileName = "New Monster", menuName = "Monster Index")]
    11. public class MonsterSkeleton : ScriptableObject
    12. {
    13.  
    14.     new public string name;
    15.  
    16.  
    17.     public int HP;
    18.     public int STR;
    19.     public int INT;
    20.     public int DEF;
    21.     public int SKL;
    22.     public int SPD;
    23.  
    24. }
    25.  

    I have made sure the inspector has the necessary fields filled out with the text UI. The two gameObjects in the world have this script attached to them, and they both have a different "Monster" scriptable object attached to them with different stats.

    I have been reading about OnValidate(), however I'm not sure how to use it correctly or if this is the solution to my problem. Thanks in advance for any and all help!!