• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_5ZilTRxtsFq7bg · May 23, 2020 at 11:51 PM · getcomponentinstantiate prefabbegginer

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image enerology · May 24, 2020 at 12:06 AM 0
Share

Oh I was too slow.

avatar image unity_5ZilTRxtsFq7bg · May 24, 2020 at 02:29 AM 0
Share

Thanks a ton tho :D

avatar image SteenPetersen unity_5ZilTRxtsFq7bg · May 24, 2020 at 06:33 AM 0
Share

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!

avatar image unity_5ZilTRxtsFq7bg SteenPetersen · May 24, 2020 at 08:11 AM 0
Share

Done! Sorry about that, I'm also fairly new to all of this! Thanks for your answer though, your solution helped me a LOT!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh Renderer not showing in scene, disabled in Inspector, but debug says it's enabled? 1 Answer

GetComponent searches an objects child instead of the object 2 Answers

Why won’t GetComponent ().rotation.SetLookRotation actually change the object’s rotation? 1 Answer

GetComponent bool changes original script 0 Answers

Adjusting size of a halo in C# 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges