- Home /
How to tell a prefab to instantiate itself?
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?
$$anonymous$$aybe I'm missing something - but when it dies, why don't you just move it back to the spawn point? $$anonymous$$aybe after a period of deactivation - you certainly don't want to be using Instantiate when you don't have to...
i 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
Why 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.
I'm quite lost.. $$anonymous$$aybe this? class Enemy { public GameObject EnemyPrefab; ... ... Die() { ... GameObject newEnemyGO = Instantiate(EnemyPrefab) as GameObject; Enemy enemy = newEnemyGO.GetComponent(); enemy.EnemyPrefab = EnemyPrefab; Destroy(gameObject); } }
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
Answer by whydoidoit · Mar 26, 2013 at 12:38 PM
You could load the prefab using Resources.Load and putting it in a Resources folder - then all you need is its name, rather than an object reference.
Your answer
Follow this Question
Related Questions
How to instantiate within GameObject 1 Answer
Referencing gameObject from script after Instantiate 0 Answers
autolink to prefab? 0 Answers