- Home /
Spawning enemies with Prefab script values
I just wanted to ask if you can Instantiate a Prefab with the prefabs own script values. I have a Character stat class in an enemy and I am trying to make the Stats go up when a spesific time is passed.
public class Stat
{
public int BaseValue;
private int Value;
public int GetValue()
{
return Value;
}
public void SetValue(int value)
{
Value = value;
}
public void AddValue(int value)
{
Value += value;
}
}
public class CharacterStats:MonoBehaviour
{
private int Health;
private int CurrentHealth;
public int BaseHealth = 100;
public Stat Damage;
public Stat Agility;
public Stat Vitality;
void Awake()
{
Damage.SetValue(Damage.BaseValue);
Agility.SetValue(Agility.BaseValue);
Vitality.SetValue(Vitality.BaseValue);
Health = BaseHealth * Vitality.GetValue();
CurrentHealth = Health;
}
void start()
{
}
void update()
{
}
public void TakeDamage(int damage)
{
CurrentHealth-=damage;
}
public int GetCurrentHealth()
{
return CurrentHealth;
}
public int GetHealth()
{
return Health;
}
}
but when I am Instantiating the prefab it seems like it has its own values is there a way to force the prefabs values to the Instantiated object.
public IEnumerator SpawnEnemy()
{
while (true)
{
var index = Random.Range(0, Enemies.Count);
Instantiate(Enemies[index], transform.position, transform.rotation);
yield return new WaitForSeconds(2);
}
}
Answer by TreyH · Apr 24 at 08:16 PM
There are a few ways to go about this, but you might find it helpful to have some sort of Initialize
function on your enemies for this purpose.
public void Initialize() // <-- add whatever values you need
{
// Assign those values
}
which you'd then call within your spawner thing:
public IEnumerator SpawnEnemy()
{
while (true)
{
var index = Random.Range(0, Enemies.Count);
var enemy = Instantiate(Enemies[index], transform.position, transform.rotation);
// Provide your values here
enemy.Initialize();
yield return new WaitForSeconds(2);
}
}
For the "make stats go up" part, you may also want to consider keeping a reference to whatever was spawned. That will make it a bit simpler to modify whatever enemies are still alive after a certain amount of time etc.
Coroutines don't return values, but you can pass a callback for when certain events happen to help with these things.
public IEnumerator SpawnEnemy(System.Action<Enemy> onEnemySpawned)
{
while (true)
{
var index = Random.Range(0, Enemies.Count);
var enemy = Instantiate(Enemies[index], transform.position, transform.rotation);
// Provide your values here
enemy.Initialize();
onEnemySpawned?.Invoke(enemy);
yield return new WaitForSeconds(2);
}
}
Thank you for the answer ! I didn't know I could pass a callback in IEnumerator as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613055757im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SteamVR - Spawning Prefab on Controller,SteamVR - Spawning Objects on Controller 0 Answers
How to randomly spawn objects above the ground? 1 Answer
What is the best way of spawning a customisable gameobject over a network? 0 Answers
Spawning Terrain Around The Player 0 Answers
A warning before a enemy spawns 1 Answer