hi! i have this enemy that has a certain number of lives, each time it dies i want it to respawn until it runs out of lives, it’s a complex enemies full of properties and states so i can’t simply give him full health again.
i thought i could simply keep a reference on itself OF itself, so lets say prefab type ENEMY has a reference for ENEMY, so when it dies i create a new one using that reference, the problem is, the reference is cyclic, because it’s referencing itself when i instantiate a new enemy using that reference i’m actually spawning the same enemy, so all the states and properties come with it, this happens because the prefab is referencing itself, but if i don’t use that reference in the prefab and simply change it on a already placed enemy, now the new enemy is an instance of the prefab, BUT since now i don’t have the reference in the prefab the new enemy borns without it so i lose my reference and i can’t respawn it again
how can i assign a reference for a prefab in script or how should i solve my problem?
Maybe I'm missing something - but when it dies, why don't you just move it back to the spawn point? Maybe after a period of deactivation - you certainly don't want to be using Instantiate when you don't have to...
– whydoidoiti simply can't, the enemy is complex, it loses parts, creates stuff and jumps from states to states, i really need to create a new one with all the initial conditions
– kebrusWhy you don't reset it to its initial condition ? It's the best solution. I approve the whydoitdoit solution and it's the more optimized.
– lisztoI'm quite lost.. Maybe this? class Enemy { public GameObject EnemyPrefab; ... ... Die() { ... GameObject newEnemyGO = Instantiate(EnemyPrefab) as GameObject; Enemy enemy = newEnemyGO.GetComponent<Eenemy>(); enemy.EnemyPrefab = EnemyPrefab; Destroy(gameObject); } }
– Tasty_Risotto@tasty that would work, but that way you are referencing itself and the new instance gets the same properties of the the actual state of the prefab, and not it's initial settings
– kebrus