• 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 wiz_0922 · Feb 17, 2020 at 09:21 PM · 2d-platformerscene-loading2d-physics2d sprites2d-gameplay

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.

Comment
Add comment · Show 5
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 Magso · Feb 17, 2020 at 10:15 PM 0
Share

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?

avatar image DCordoba · Feb 18, 2020 at 02:00 AM 0
Share

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


avatar image tormentoarmagedoom DCordoba · Feb 18, 2020 at 02:07 AM 0
Share

what? I think this is not what is he asking for

avatar image DCordoba tormentoarmagedoom · Feb 18, 2020 at 02:16 AM 0
Share

=.=) 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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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
0

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

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

141 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

Related Questions

How to create soil or sand?? 1 Answer

How To Make Game Remember User Input Then Play It Out, But Backwards 4 Answers

Why Is there A slight jitteryness on player movement and camera? 0 Answers

Death Counter dont work when changing scenes 2 Answers

Transform.position assign attempt not valid, and position is infinity on an object? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges