So, I’m working on my first project and came across my first GREAT impasse. I’m creating an RPG, and like those old-school games when you touch an enemy it triggers the turn-based fight that may or may not contain more enemies. I’ve done almost everything regarding the setup and RNG numbers, but my problem is the following:
I have a main script attached to an empty Object called BattleManager, from here I instantiate enemies to the board in their corresponding platforms, enemies have a fixed spawn percentage and can vary depending on an “Ally” system. For instancing the main enemy I use the following:
MainEnemy = (GameObject)Resources.Load("Enemy/" + EnemyStatic.EnemyCode, typeof(GameObject));
FirstStation.SetActive(true);
Instantiate(MainEnemy, EnemyStation1);
This looks for the enemy you encountered from a library of prefabs and instantiates it on the first platform. This works great and I use the “overworld” enemy to pass the level value that alters a logarithmic multiplier for health and attack and so. My problem arrives when I want to alter the values from my MainEnemy (or any enemy for that matter)
I use the following command to alter values belonging to my Enemy script:
MainEnemy.GetComponent<EnemyController>().IsUp = true;
Now, using the “GetComponent” works wonders when calling a function inside of the EnemyController from the BattleManager but when it comes to alter the variables it just doesn’t work.
Note that all of the variables from the EnemyController script are public (I tried Static but changes every enemy on the battlefield). I tried unparenting the enemies, callling a function inside EnemyController that changes the variable and creating code shortcuts but none of those worked for me.
I wonder, Is there a way to change an instantiated prefab object’s values from another script?