Home>

I wrote the following code to respawn the player.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnkomanRespawn: MonoBehaviour
{
    float RespawnTime;
    void Start ()
    {
        RespawnTime = 0f;
    }
    void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.tag == "Sea")
        {
            this.gameObject.SetActive (false);
        }
    }
    void Update ()
    {
        if (this.gameObject.activeSelf == false)
        {
            RespawnTime + = Time.deltaTime;
            Debug.Log ("RespawnTime");
        }
        if (RespawnTime == 1f)
        {
            this.gameObject.SetActive (true);
            RespawnTime = 0f;
        }
    }
}


When the player character touches the object "Sea", I want to respond to the object one second after dying.
However, it does not come back after it has been inactive by touching "Sea".
I think it's probably because the script is stuck because it's inactive, but I don't know what code I can write to make it work.
I googled and saw the code that came out, I tried copying and copying it, but it didn't work.
Please let me know what code can be added to the above code to make it work.

  • Answer # 1

    The player is inactive, so the script does not run.
    Adding this code doesn't work.

    Attach scripts to other objects instead of players,
    From there, you should switch between active and inactive players.

    For example, get the player before it disappeared with the GameObject.Find method from the script attached to another object,
    You can measure Respawn Time with Update and activate the player.

  • Answer # 2

    As you might guess, the GameObject becomes inactive and can't move from you.
    I think it would be good to redesign the design.

    Whether to separate the script that manages the respawn from the player itself
    I think that there are various methods such as cutting the drawing and making it look like it is not transparent, rather than deactivating it, so please consider according to the specifications of the game you are making

Related articles