- Home /
[C#] How to spawn multiple objects that share the same base-class
My Problem: I don't know where to set my bool spawnFlag to false. If I set it within my base-class, only one object is respawned properly. If I don't set it at all, with every update all the objects are respawned (which means technically the respawn logic itself is working, but it needs to stop after the respawn of those objects). I may have gone about this all wrong, but with how I have it laid out currently, I just need to figure out how to properly end the spawning of the objects inheriting my classObject base-class once my main character spawn triggers.
I have a GameManager class that has a method to spawn the main character.
public void SpawnPlayer(Vector3 spawnPos) {
spawnFlag = true;
currentPlayer = (Instantiate(player,spawnPos,Quaternion.identity) as GameObject);
cam.SetTarget(currentPlayer.transform);
}
The other characters need to be respawned at their default spawn position upon respawn of the player. The other characters inherit the base-class 'classObject'. Within this is a method that is called in the Update() of the sub-class to allow respawning.
//Clears trigger flag and sets position to default
public void AllowRespawn() {
if (gm.spawnFlag) {
Debug.Log("Respawning Object: " + this.gameObject);
isTriggered = false;
transform.Translate(Vector3.zero);
transform.position = spawnPos;
}
}
Answer by Jiro-Ng · Mar 07, 2014 at 08:08 AM
I think the re-spawning need to perform by the Game Manager or the base class but not withing the character itself. You can try to do in Game Manager. When spawnFlag is true, find all character object, then respawn with a for
or foreach
loop. This is just my suggestion. Hope this fits your game.
Sounds about right. I don't see any use for the spawnFlag here. Just give the game manager control over all your instances.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135609im_/https://answers.unity.com/themes/thub/images/avi.jpg)