- Home /
Change a variable on a prefab in unity 5 js
public var ZombiePrefab: GameObject;
ZombiePrefab.GetComponent("ZombieScript").health = health + 10;
I want to change the health of the prefab so that when the next wave of zombies starts they have more health. this code doesn't work so can anyone tell me how to fix this code or just how to change the value of the variable.
Answer by FortisVenaliter · May 04, 2016 at 05:35 PM
You generally do not want to change prefabs at runtime. For example, if your game gets rebooted, should it keep the variable you set or the one it's built with?
What I would do instead would be to write a WaveManager script that each zombie would check with when they're instantiated, and set their own health based on the current wave index.
I am not verry good with getting variables from other gameobject so could you show me how I could change the variable of all the zombies on the map that have the Zombie tag?
var Zombies = GameObject.FindGameObjectsWithTag("Zombie");
Zombies.GetComponent("ZombieScript").health = health + 50;
I tried this but it doesn't work
Your answer
Follow this Question
Related Questions
Creating a Prefab and Keeping Original Objects Script Information 0 Answers
How to discard all changes to public variables when the game ends? 1 Answer
Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers
Prefab not changing variable's values 2 Answers
I've attached a prefab to a script, but it still says the variable has not been assigned 1 Answer