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;
}
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.
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.
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")