Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 burchland2 · Aug 31, 2021 at 01:40 PM · 2d gamerespawngame over

How to respawn player game object after game over

Hi,

I am trying to respawn the player after pressing the continue button and hiding the game over screen. Here is the code so far.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class UnitManager : MonoBehaviour
 {
     public PlayerController ball;
 
     public float respawnDelay = 5;
 
     public GameObject currentCheckpoint;
 
     public GameStatus gs;
 
     private CountdownTimer ct;
 
     public GameObject destroyParticle;
 
     public GameObject respawnParticle;
 
     private float gravityStore;
 
     private HealthManager hm;
 
     public LivesSystem ls;
 
     public CameraController cam;
 
     public Reset res;
 
     public GameObject gameOver;
 
     void Start()
     {
         ball = FindObjectOfType<PlayerController>();
         gs = FindObjectOfType<GameStatus>();
         ct = FindObjectOfType<CountdownTimer>();
         hm = FindObjectOfType<HealthManager>();
         ls = FindObjectOfType<LivesSystem>();
         cam = FindObjectOfType<CameraController>();
         res = FindObjectOfType<Reset>();
     }
 
     public void RespawnPlayer()
     {
         hm.DestroyPlayer();
         if (gs.lives >= 2)
         {
             StartCoroutine("RespawnPlayerCo");
         }
         else
         {
             Over();
         }
     }
 
     public void Over()
     {
         StartCoroutine("OverCo");
     }
 
     public IEnumerator RespawnPlayerCo()
     {
         Instantiate(destroyParticle, ball.transform.position, ball.transform.rotation);
         ball.enabled = false;
         ball.GetComponent<Renderer>().enabled = false;
         cam.isFollowing = false;
         //gravityStore = ball.GetComponent<Rigidbody2D>().gravityScale;
         //ball.GetComponent<Rigidbody2D>().gravityScale = 0f;
         //ball.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
         Debug.Log("Player Respawned: " + transform.position);
         yield return new WaitForSeconds(respawnDelay);
         //ball.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
         ball.transform.position = currentCheckpoint.transform.position;
         ball.enabled = true;
         ball.GetComponent<Renderer>().enabled = true;
         hm.Revive();
         cam.isFollowing = true;
         Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
     }
 
     public IEnumerator OverCo()
     {
         Instantiate(destroyParticle, ball.transform.position, ball.transform.rotation);
         cam.isFollowing = false;
         ball.GetComponent<Renderer>().enabled = false;
         //gravityStore = ball.GetComponent<Rigidbody2D>().gravityScale;
         //ball.GetComponent<Rigidbody2D>().gravityScale = 0f;
         //ball.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
         yield return new WaitForSecondsRealtime(3);
         gameOver.gameObject.SetActive(true);
     }
 
     public void DoOver()
     {
         StartCoroutine("Again");
     }
 
     public IEnumerator Again()
     {
         gameOver.gameObject.SetActive(false);
         Debug.Log("Player Respawned: " + transform.position);
         yield return new WaitForSeconds(1);
         //ball.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
         ball.transform.position = currentCheckpoint.transform.position;
         ball.enabled = true;
         ball.GetComponent<Renderer>().enabled = true;
         hm.Revive();
         cam.isFollowing = true;
         Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
     }
 
     public void StartOver()
     {
         SceneManager.LoadScene("Intro");
     }
 }

Any assistance would be appreciated.

Sincerely,

burchland2

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Unrighteouss · Aug 31, 2021 at 03:29 PM

Make the player a prefab, then Instantiate the prefab. I can see that you've already used Instantiate a bunch, so you probably won't have trouble doing that.

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 burchland2 · Aug 31, 2021 at 06:13 PM

I tried:

Instantiate(ball); Instantiate(player); I assigned the player to both the ball and player variables. (I already made the player a prefab. Instantiate(ball, ball.transform.position, ball.transform.rotation); Instantiate(player, ball.transform.position, ball.transform.rotation); The latter was a small mistake on my part. But the same thing occurred with no player popping up. What else should I try? Is there something that I'm missing? Thank you.

Sincerely,

burchland2

Comment
Add comment · Show 8 · 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 Unrighteouss · Aug 31, 2021 at 06:20 PM 0
Share

Make sure that you set the spawn position and rotation in the Instantiate method, not the object's position/rotation.


That being said, it's difficult to understand what the issue is. Does the player not appear? Does Instantiate not work? Add debug lines to see what's happening, and why the behavior isn't what you expect: https://docs.unity3d.com/ScriptReference/Debug.Log.html

avatar image burchland2 Unrighteouss · Aug 31, 2021 at 08:51 PM 0
Share

Sorry for not specifying, but the player does not appear. The source of the problem was that, in the hierarchy, the clone was not selected, but it was grayed out.

avatar image Unrighteouss burchland2 · Aug 31, 2021 at 09:49 PM 0
Share

So you're saying the player starts inactive? If that's the case, you just need to set the player to active:

 [SerializeField] GameObject player;
 [SerializeField] Transform spawnPoint;
 
 void Start()
 {
     GameObject newPlayer = Instantiate(player, spawnPoint.position, Quaternion.identity);
 
     newPlayer.SetActive(true);
 }
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

169 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

Related Questions

Why doesnt my respawn script work? 2 Answers

Respawn back at the old position. 1 Answer

How can I change rotation of spawning platforms?(platforms must appear at angles of -45 degrees Y),How can change rotation of platforms? (plantforms must appear at angles of -45 degrees Y) 1 Answer

Vector Graphics versus Raster Graphics in Unity 2019.3 2 Answers

How can I change the character for the gameplay after selecting the character? 2 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