Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DanProd · Feb 16, 2019 at 04:09 AM · scripting problemscript.scriptingbasicsspawningscipting

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;

 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GenericToast · Feb 16, 2019 at 04:45 AM 1
Share

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

avatar image DanProd · Feb 16, 2019 at 07:50 AM 0
Share

Thank you all Thank you all!

avatar image zereda-games DanProd · Feb 16, 2019 at 02:26 PM 0
Share

Upvoting helps us all ;)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Link


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")


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dan_wipf · Feb 19, 2019 at 11:03 AM 0
Share

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!

avatar image zereda-games dan_wipf · Feb 19, 2019 at 11:23 AM 1
Share

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!"

avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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);
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zereda-games · Feb 16, 2019 at 06:06 AM -2
Share

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.

avatar image sean244 zereda-games · Feb 19, 2019 at 12:26 AM 0
Share

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.

avatar image zereda-games sean244 · Feb 19, 2019 at 06:44 AM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reduce my script ? 0 Answers

Trying to find the highest number than add it to itself. 2 Answers

How would i make it transform player.position =target.position? 2 Answers

Point Counter Works Only Once! 1 Answer

how to allow the key to only open 1 door rather than all of them? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges