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

Accessing information (connected to enumerations?) inside other game objects

Discussion in 'Scripting' started by maxmcarthur, Jan 25, 2015.

  1. maxmcarthur

    maxmcarthur

    Joined:
    Jan 25, 2015
    Posts:
    3
    Hello,
    I am trying to access information in game objects to fill in information on GUI windows. The information I am trying to retrieve includes getting lists of enumeration entries, then also retrieving secondary information attached to those entries through other scripts (enumerations and other scripts all reside on every game object in question).

    Sorry for the length of this in advance, I'm trying to provide as much relevant information as I can think of, and thank you if you even attempt to read through this mess but can't help. Hopefully I describe everything accurately, but I might make a mistake due to lack of knowledge / being a beginner.

    The code in question is:
    Code (CSharp):
    1.         //display the attributes
    2.         GameObject[] player = GameObject.FindGameObjectsWithTag("Player");
    3.  
    4.         for( int i = 0; i < CharacterStatsPassiveName.GetValues(typeof(CharacterStatsPassiveName)).Length; i++ ) {
    5.             GUI.Label( new Rect( 0, i * lineHeight, _characterWindowRect.width - ( _offset * 2 ) - valueDisplayWidth - 5 , 25 ), ((CharacterStatsPassiveName)i).ToString() );
    6.             //GUI.Label( new Rect( _characterWindowRect.width - ( _offset * 2 ) - valueDisplayWidth, i * lineHeight, valueDisplayWidth , 25 ), GetPassiveAttribute( i ).StatBase.ToString() );
    7.         }
    8.  
    The un-commented parts of this code do work, however they are currently accessing the global enumeration data and displaying that in the GUI, whereas I am trying to access instantiated versions of the enumeration and subsequent data (for example, in the commented-out line, GetPassiveAttribute( i ).StatBase.ToString() fails to work due to no link to anything).

    My goal in the specific example above is to display the Player's stat information in the GUI, and I'm wondering what syntax would be necessary for this (and any related variables / code that might be required to sustain the request). I only started learning to code a few weeks ago so I'm having trouble wrapping my head around this.

    This code is code somewhat modified from BergZergArcade's RPG tutorial series in terms of specific variable names. His example for this code is:
    Code (CSharp):
    1.         //display the attributes
    2.         for( int cnt = 0; cnt < PC.Instance.primaryAttribute.Length; cnt++ ) {
    3.             GUI.Label( new Rect( 0, cnt * lineHeight, _characterWindowRect.width - ( _offset * 2 ) - valueDisplayWidth - 5 , 25 ), ((AttributeName)cnt).ToString() );
    4.             GUI.Label( new Rect( _characterWindowRect.width - ( _offset * 2 ) - valueDisplayWidth, cnt * lineHeight, valueDisplayWidth , 25 ), PC.Instance.GetPrimaryAttribute( cnt ).BaseValue.ToString() );
    5.         }
    6.  
    PC.Instance seems to involve set up through other scripts, any attempts I tried to mimic this type of call to the instance failed and the specific setup he uses don't seem applicable to my goals (statistic screens for multiple characters depending on which is selected, where his seems intrinsically linked to PC.Instance only).


    Finally I'll show a different set of code I created myself (not from the BergZerg tutorials) that allowed me to access information similar to what I desire in the GUI script, but I can't figure out how to apply that knowledge to the GUI script.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestDamageAll : MonoBehaviour {
    4.  
    5.     public void HurtUnits(){
    6.         GameObject[] enemy = GameObject.FindGameObjectsWithTag("Enemy");
    7.         GameObject[] player = GameObject.FindGameObjectsWithTag("Player");
    8.         Debug.Log (enemy.Length);
    9.         Debug.Log (player.Length);
    10.  
    11.         foreach(GameObject Enemy in enemy){
    12.             Enemy.GetComponent<ActorEnemy>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue = (Enemy.GetComponent<ActorEnemy>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue) - 5;
    13.             Enemy.GetComponent<ActorEnemy>().GetActiveAttribute((int)CharacterStatsActiveName.Health).Update();
    14.             Debug.Log(Enemy.GetComponent<ActorEnemy>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue);
    15.         }
    16.  
    17.         foreach(GameObject Player in player){
    18.             Player.GetComponent<ActorHero>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue = (Player.GetComponent<ActorHero>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue) - 5;
    19.             Player.GetComponent<ActorHero>().GetActiveAttribute((int)CharacterStatsActiveName.Health).Update();
    20.             Debug.Log(Player.GetComponent<ActorHero>().GetActiveAttribute((int)CharacterStatsActiveName.Health).CurValue);
    21.         }
    22.     }
    23. }
    The script above searches for Player-tagged objects (there's only 1) and Enemy-tagged objects (as many as I want seems to work), then accesses their CharacterStatsActive enumeration and gets .Health specifically, then the current value which is derived from other scripts in the inheritance, then finally it lowers the current value, effectively damaging all units on screen. This was my first test at accessing the information and it works perfectly.

    Finally one last piece of working code to try and demonstrate the usage, this piece in question is attached directed to the player object, unlike the GUI and HurtUnits examples, this code displays the enumeration entry name, then the two related variables that displayed current Health and maximum Health.
    Code (CSharp):
    1.             for( int i = 0; i < Enum.GetValues(typeof(CharacterStatsActiveName)).Length; i++ )
    2.                 GUI.Label( new Rect( Screen.width - 120, 10 + (i * lh) + ( Enum.GetValues(typeof(CharacterStatsPassiveName)).Length * lh ), 300, lh ), ((CharacterStatsActiveName)i).ToString() + ": " + GetActiveAttribute(i).CurValue + "/" + GetActiveAttribute(i).AdjustedBaseStat );
    3.  

    So in summary, I'm trying to figure out how to display information similar to the final example (HP: 13 / 18) in the original example that is a GUI script attached to a empty game object that is trying to access information from a single Player-tagged object, and hopefully from more objects after I learn how to access the code successfully.

    Thank you.
     
  2. maxmcarthur

    maxmcarthur

    Joined:
    Jan 25, 2015
    Posts:
    3
    After messing around with it for a few more hours, I rewrote the thing from scratch and then thereafter realized the huge mistake I made after trying to type Enum.stuff in the script to test. At some point I managed to remove "using System;" from the original file (or I started the file from a blank maybe), which seems to be absolutely required.

    Being a novice is hard.

    Thanks and sorry if you spent time reading the above, this should be closed probably.