Understanding Game Objects and How to Access Variables from Other Scripts?

Hey guys, so i’m working on creating a game with multiple units on a battlefield and am currently trying to implement an xp system that awards xp to the specific units that fight in a battle (think fire emblem xp system). However, I am having trouble understanding how to reference the xp variables from my character scripts and how they all work when attached to specific game objects? Also, if anyone could help me understand how to reference the specific unit involved in a battle when I call the xp function, that would be a huge help (something like using “this” in javascript)! I searched through all the tutorials I could find about this last part and they all seem to focus on games where there is only one player character, so there is only one exp variable to work with (which makes things way simpler :confused: ) Thanks in advance for the advice!

I’m not familiar with fire emblem, but if each unit has its own XP during battle … they could each have their own script which can store that XP data. I could say more, but first - is that correct, each individual unit has separate xp? or is it by groups or?

In java, you can collect all the units into an array of GameObjects, and use a for each loop:

for(GameObject unit : units)
{
unit.GetComponent().xp+=value;
}

Also make sure the xp variables are set to public.

Yep, you can do that in C#, as well.

foreach(GameObject unit in units)
{
unit.GetComponent<Script>().xp += value;
}
// or
for(int u = 0; u < units.Length; ++u){
unit[u].GetComponent<Script>().xp +=value;
}
// ** if Units is an array; otherwise (if it's a List) : "units.Count" in the loop above.

Using a list would probably be a better idea. Then you don’t have to worry about a fixed size of units.

Yep. Auto-size managed. That everything you needed? :slight_smile:

Wow, thanks guys, I actually figured that I can write an exp and leveling function into the base class that all units inherit from and call it using “this” whenever necessary (I hope this isn’t a bad idea lol), but I appreciate the feedback and this will definitely be useful to reference down the road!

No problem. And for the record, “.this” isn’t required, unless the statement is ambiguous. :slight_smile:

Hey so I’m actually running into a little snag that I can’t quite seem to get past and was wondering if any of you guys could take a quick look and see what I’m doing wrong here? So this is the base class with the exp function:

public class BaseCharacterClass {

private int currentExperience; /* current exp of each individual character */ /* i have getters and setters just not posting them rn */
    private int characterLevel;

public void AddExperienceOnKill()
    {
        xpToGive = this.characterLevel + 20; /* determine xp by calculating this.level with enemyfought.level or something */
        this.currentExperience += xpToGive;
        if (this.currentExperience >= 100)
        {
            this.LevelUp();
            this.currentExperience = this.currentExperience - 100;
        }
    }

private float lifePointsGrowth;
    public float LifePointsGrowth {get; set;}

public void LevelUp()
    {
        this.characterLevel += 1;
        if (this.lifePointsGrowth >= Random.Range(0.0f, 1.0f))
        {
            this.lifePointsMax += 1;
        }
    }
}

And then I instantiate a class somewhere else, set a new value for the lifePointsGrowth float and then try to call the levelup function, but it doesn’t work (i have a hunch that the this in the BaseCharacterClass code is using the variables defined in baseCharacterClass instead of my other child class (code below), so the values always come out as 0, which is what they default to) :confused:

public class NovaPerson : MonoBehaviour {

    public BaseArcherPlayerClass newNova; /* inherits from base player in another script */

    // Use this for initialization
    void Start() {
        CreateNova();
        Debug.Log("Current Exp: " + newNova.CurrentExperience);
        Debug.Log("Current Level: " + newNova.CharacterLevel);
        Debug.Log("Current Health: " + newNova.LifePointsMax);
        newNova.AddExperienceOnKill();
        newNova.AddExperienceOnKill();
        newNova.AddExperienceOnKill();
        newNova.AddExperienceOnKill();
        newNova.AddExperienceOnKill();
        Debug.Log("Current Exp: " + newNova.CurrentExperience);
        Debug.Log("Current Level: " + newNova.CharacterLevel);
        Debug.Log("Current Health: " + newNova.LifePointsMax);
    }
}

Edit: I think I made it work actually, I just realized that in the Base Character script that since I wasn’t calling the getter/setter with the capital first letter, that it wasn’t setting correctly, so by changing that I think I fixed the problem! Thanks again to you guys for helping me though! Have a great night

I was reading your post & your code and then I got to the bottom and read your edit lol. So it’s working? :slight_smile: Good stuff.

Yup, thanks again for the help!

:slight_smile: