Begginer Question on instantiate.

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?

The problem arises because you are trying to access a component on an entity, ‘MainEnemy’, in your resources, not in the scene. As you can see from the line below ‘MainEnemy’ is referring to the object you are finding in resources, not the instantiated object.

MainEnemy = (GameObject)Resources.Load("Enemy/" + EnemyStatic.EnemyCode, typeof(GameObject));
FirstStation.SetActive(true);

To fix this we can simply make a temporary variable that we can refer to when actually instantiating the enemy, like so:

var tmpEnemy = Instantiate(MainEnemy, EnemyStation1); // temporary holder for the enemy
EnemyController ec = tmpEnemy.GetComponent<EnemyController>(); // reference to the script
ec.changewhateveryoulike // make your changes

I might be seeing this wrong but it looks like when you are changing the prefab values instead of the instantiated version.

For instance, you set MainEnemy to the prefab in the line

 MainEnemy = (GameObject)Resources.Load("Enemy/" + EnemyStatic.EnemyCode, typeof(GameObject));

and you call the GetComponent on that prefab.

Instead, you want to store a reference to the enemy you just instantiated like

Gameobject tempMainEnemy = Instantiate(MainEnemy, EnemyStation1);
//Then Call Get Component On This
tempMainEnemy.GetComponent<EnemyController>();

Good luck on your project.