- Home /
Spawn A prefab after death
Im new to scripting and I want to know just how to spawn a prefab before the deletion of my object//player. Like blood/player death animation.etcetc (i also want it to spawn where the player died not in world x:0 y:0 z:0
here is the code:
public int maxHealth = 100;
public int currentHealth = 100;
public Text Maintext;
public Transform DeadPrefab;
void Start()
{
currentHealth = maxHealth;
}
void Update()
{
Maintext.text = currentHealth.ToString();
if (currentHealth <= 0)
{
Destroy(this.gameObject);
}
}
public void DamagePlayer(int Hurt, Vector3 direction)
{
currentHealth -= Hurt;
}
public void HealPlayer(int Aid, Vector3 direction)
{
currentHealth += Aid;
}
Spawn your DeadPrefab in a function called OnDestroy(). This function gets called when the gameobject gets destroyed. https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnDestroy.html
Answer by zereda-games · Feb 16, 2019 at 05:45 AM
Well there were error's Could still be! It's hard to type in such a small box on a phone, but that's why we post and get corrected right? so we can edit and change our answers to better help the questioner. I'm not a professional coder, not am i afraid of being corrected, but i'm deffinatly not a beginner anymore.. not after 3 yeas of on and off again doing this. There is no such thing as a stupid question, and bad answers can be corrected. if one is willing I have noticed that people haven't grasped the concept of the Answers section yet and use it like the forums. Everyone should go read the FAQ if the have not done so already. This isn't second grade class, most of us are adults even if some of us don't want to admit it, were all are hear to learn and help others, so play nice people and good day.
Tip:
and make sure the prefab is in A resources folder, doesn't matter which one, just needs to be inside one. if it is inside another folder inside the resources folder call if by:
go = (GameObject) Resource.Load("FolderName/ObjectPrefabName")
sorry, for offtopic comment here, but i’m defently on your side, what unity answers is, and is not. as we learnt in school there are no stupid question’s only stupid answers(replies)
i ‘d recommend anyone the stackoverflows faq about answering nice and friendly (CodeOfConduct
i have to admit that i sometimes use letmegooglethatforyou but those cases are just realy duplicates and not well searched questions!
Thanks, 100% agree with that comment. I was suggested to read Unity Answers FAQ, and i did and Actually learned something!!! Believe it or not that's kinda where that comment came from JSY$$anonymous$$. And sort of realized why i had complaints about comments or answers because i may have been off topic or commenting too much was one.. All i had to say to That was, "Well if one doesn't pick another's brain about something, how are others going to deter$$anonymous$$e what had been tested or not. Especially if the question if vague?" I also said something like, "If no one answered a question, or got corrected when they did, no one would ever get an answer!"
Answer by sean244 · Feb 16, 2019 at 05:00 AM
Create an event inside of the Player script that is invoked when the player's health reaches zero. Then create a function inside of your GameManager that's responsible for instantiating the object that you want. This function should subscribe to the event that's invoked inside of your Player script. You can even have the event pass over the player's transform position, so that the function inside of the GameManager knows exactly where to instantiate the object.
Answer by James_BadAlchemy · Feb 16, 2019 at 04:59 AM
if (currentHealth <= 0) {
Vector3 deathPos = this.gameObject.transform.position;
Destroy(this.gameObject);
GameObject prefabToSpawn = Instantiate(somePrefab, deathPos, Vector3.zero);
}
that won't work as the game object gets destroyed before the prefab is instantiated.
[Edit]
if i delete this comment my valid correction by @HomebrewAI will be lost.
That's not true. The gameObject does not get destroyed immediately. It gets destroyed at the end of the frame. The reason his code won't work is because he wrote 'Vector3.zero' ins$$anonymous$$d of 'Quaternion.identity'. If you make that change, his code will work.
10 4 see this is why i'm on Answers @sean244 I get corrected, i learn. I don't have a physical $$anonymous$$cher like many of you had.. i'm not that fortunate. I try my best and it isn't always correct or Everyone else' way.. But at least i try. If no one tries no one gets an answer!