- Home /
How do instantiate a ghost prefab when the player dies?
I am making a 2d game and am trying to drop a ghost prefab when the player dies that he has to collect to gain his powers back. Any help on how to implement this would be greatly appreciated!
When the player dies I am reloading the scene if that helps at all.
What order do "when the player dies that he has to collect to gain his powers back" and "when the player dies I am reloading the scene" happen in?
create a Script with DontDestroyOnLoad(this.gameObject); on Awake(), attach it to the "ghost"
use OnDestroy() inside the player script, when it dies(is destroyed) instantiate the object with the script and reload scene
other easy way, you can do this after instantiate it without a additional script, inside the player script:
OnDestroy()
{
GameObject instanceGhost = (GameObject)Instantiate(prefabGhost);
DontDestroyOnLoad(instanceGhost);
}
what? I think this is not what is he asking for
=.=) he is reloading the scene, he need to preserve the data of the powers gained on this round (the current scene) and transport this to the next... you see what is related now? he create the ghost of the current round on current scene and preserve it while load the next...
well, I forgot to add some to check if he is not loading another scene and, on this case, don't load the ghost, to do this he probably he have to move this script from OnDestroy
to the part where he decide if reload or load, by example, next level.
Answer by tormentoarmagedoom · Feb 18, 2020 at 02:12 AM
Hello.
You are not asking a specyfic question.
To your question, I answer:
1- Create the ghost prefab object
2- When player dies, Instantiate the ghost prefab in the position of the player
3- Move player transform to the respawn point.
Bye.
Answer by utzsar · Feb 18, 2020 at 04:07 AM
tormentoarmagedoom is right, this isn't really a technical question, but a stylistic one. There are lots of ways you could do this, it all just depends on the breadth of what you want to accomplish. Personally I wouldn't even go through the trouble of instantiating a new prefab for the player, I'd just have "living" and "ghost" settings on the main player object that activate/deactivate depending on the state.
Why does the scene reload on player death? Is it the same scene or a new scene? Either way, when the scene starts up, you could just have a different player object active depending on if the player has died or not, so you wouldn't even have to "drop a ghost prefab". But here's how prefab spawning works if you're unaware of it, or rather, one way you can go about it.
// Make a reference to the prefab, then instantiate a clone of the prefab.
GameObject tempChar = Resources.Load<GameObject>("*Name of your desired spawn prefab here!");
// note that you have to create the Resources folder in your assets and put the prefab there.
// It has to be a folder called Resources, but you can make nested folders called whatever you want.
// in a nested folder the part in quotations would be like "folderName/*nameofprefab*"
// You can also just set tempChar as a public variable in the script and assign it in the inspector,
// if you would rather avoid Resource loading altogether.
GameObject tempCharInstance = Instantiate(tempChar, transform.position, transform.rotation);
// set the position/rotation to wherever you like, probably your spawnpoint or previous character position.
Blockquote