- Home /
Player invisible on respawn
NOTE: ignore black box, its for testing
Initial player:
Instantiated player:
using UnityEngine;
public class DeathCheck : MonoBehaviour
{
public GameObject paintBall;
public GameObject expEffect;
public AudioSource splatSound;
public Transform shotPoint;
public Transform spawnPoint;
public float ExpDeviation;
public float cooldown;
float cooldownTime;
private void Start()
{
gameObject.name = "Player";
cooldownTime = Time.time + cooldown;
}
// Update is called once per frame
void Update()
{
Debug.Log("Time: " + Time.time);
Debug.Log("CooldownTime: " + cooldownTime);
if (Time.time > cooldownTime)
{
if (Input.GetButton("Fire1"))
Death();
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (Time.time > cooldownTime)
{
if (collision.gameObject.tag == "Deadly")
Death();
}
}
void Death()
{
Respawn();
Destroy(gameObject);
Debug.Log("Boom!");
splatSound.Play();
float rotZ, dev;
//Creates 20 balls with random rotations
for (int i = 0; i < 20; i++)
{
rotZ = i * 18 + Random.Range(-ExpDeviation / 2, ExpDeviation / 2);
transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
Instantiate(paintBall, shotPoint.position, transform.rotation);
}
//Explotion Effect
Instantiate(expEffect, shotPoint.position, transform.rotation);
}
void Respawn()
{
GameObject newPlayer = Instantiate(gameObject, spawnPoint.position, Quaternion.identity);
}
}
screenshot-2021-02-12-163006.png
(93.4 kB)
screenshot-2021-02-12-162802.png
(79.2 kB)
Comment
I think your respawning then destroying it. In void Death() move Respawn() to after splatSound.Play();
void Death()
{
Destroy(gameObject);
Debug.Log("Boom!");
splatSound.Play();
Respawn();
Best Answer
Answer by rxx1 · Feb 14, 2021 at 04:46 PM
I found the problem, my spawn point was on a different z position then my player, causing it to become invisible
Answer by highpockets · Feb 14, 2021 at 09:36 AM
I would think that a decent way to do this would be to just reposition the object instead of Destroy and Instantiate it. Though if you want to destroy/instantiate it, make the player object a prefab and instantiate that prefab and not the working instance.
[SerializeField] private GameObject player;
Respawn()
{
Instantiate(player);
}