- Home /
How to set/get/change variables for newly spawned gameObjects?
So I have prefabs in my game that will spawn and be placed by the player if they push a GUI button. I'm trying to figure out how to set the individual buildings hit points though. So, if the player creates a barracks:A and then later creates a barracks:B and say barracks:A takes some damage, how would I adjust the damage amount for the individual, in-game spawned barracks? Is it as simple as attaching a script to it like building.cs and having it's hit points inside of it adjusted under certain conditions? Will all buildings with the building.cs be effected if one is, or only the individual building? If there are any links that will explain how such scripting works I would appreciate it. I've managed to get a loose grip on how scripts can interact with each other but, of course, I'm still trying to figure several things out and the structure is proving to be difficult for me!
Answer by nicolasjr · Mar 10, 2014 at 09:12 PM
Is it as simple as attaching a script to it like building.cs and having it's hit points inside of it adjusted under certain conditions? Will all buildings with the building.cs be effected if one is, or only the individual building?
Yes
Is it as simple as attaching a script to it like building.cs and having it's hit points inside of it adjusted under certain conditions?
Yes, once you instantiate the prefab, if it already has the script, it'll be also creating a new instance of the class, so you can get a pointer to it by using GetComponent on the game object instantiated.
Will all buildings with the building.cs be effected if one is, or only the individual building?
Only the individual one, as long as you handle it properly. But no way you can affect every instance. Unless you keep track of every instance and deliberately do that.
Awesome! This helped answer a lot of my questions. Any chance you would $$anonymous$$d explaining or pointing me to material that would explain how to keep track of every instance if I wanted/needed to? (Like if my player upgrades his society and as a result, every building changes how fast they produce)
Your answer
![](https://koobas.hobune.stream/wayback/20220613135917im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change a Script Var of an Instantiated Object 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Changing a variable across multiple instances of a GameObject 1 Answer
How to save a gameObjects on a prefab and variables with a script? 1 Answer
Detect when instance has deleted 0 Answers