- Home /
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?
Answer by SteenPetersen · May 23, 2020 at 11:57 PM
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
Answer by enerology · May 24, 2020 at 12:06 AM
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.
Glad that we could help @unity_5ZilTRxtsFq7bg - please remember to mark answers that you feel have solved your issue as correct, and upvote people's answers. It shows appreciation to those that took time to answer your issue and it lets the community know that this issue has been resolved, so others can benefit from the information, and avoid navigating here if they are looking to help.
Furthermore remember to be specific with your titles so that people can find this issue in the future and learn from it. It will be more unlikely that someone will look for 'Begginer Question on instantiate.' than say, 'Problems with calling GetComponent on instantiated enemy/Object'.
Sorry for the long-winded comment, it's just good practice here and on StackOverflow as well. That being said, Welcome to the community!
Done! Sorry about that, I'm also fairly new to all of this! Thanks for your answer though, your solution helped me a LOT!
Your answer