0

I am making a really short C# Unity game for a college class I am in and I have created a script for a trap that deactivates my player on contact that also includes a replay button. It all works except when I replay, the player remains inactive. How would I modify my script to make it so the player reactivates on replay? Also, this class I am in is a beginner class, I'm not super good at this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Trap : MonoBehaviour 
{   
    public GameObject playerExplosion;
    public GameObject gameOverUI;   

    void OnTriggerEnter (Collider other)    
    {       
        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);           
        }       
        other.gameObject.SetActive(false);
        gameObject.SetActive(false);
        gameOverUI.SetActive(true);
        PlayerController.gameOver = false;
    }
}

Edit: Here is the replay script too. It works on a health bar system.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ReplayGame : MonoBehaviour
{
    public Transform player;
    public Image uiBar;
    public GameObject GameOverUI;
    public static Vector3 startPosition;
    private float fillAmount;

    void Start()
    {
        startPosition = player.position;
        fillAmount = uiBar.fillAmount;
        GameOverUI.SetActive(false);
    }

    public void Click ()
    {
        PlayerController.gameOver = false;
        player.position = startPosition;
        uiBar.fillAmount = fillAmount;
        GameOverUI.SetActive(false);       
    }   
}
  • 1
    Mind if I asked how your Replay logic works? Since it is the part that is not working as expected it would help in finding a solution. – Maglethong Spirr Nov 10 '17 at 08:24
  • @MaglethongSpirr Yea sure I will put that script in my original question since there isn't enough word space in the comments. – Cepheron Kalle Nov 11 '17 at 18:41
  • It seems to me you forgot to call `.SetActive(true)` to reactivate it. Is that possible? Or could it be `PlayerController.gameOver = false` does that for you? Note that, if you are attempting to reactivate your player inside its own script, Unity messages (such as `Update()` or `OnTriggerEnter()`) will not be called after it is deactivated with `SetActive(false)` – Maglethong Spirr Nov 13 '17 at 06:33

1 Answers1

0

you will have to re Activate your player, or you have to Instantiate a new player.

public void Replay ()
{
    //ReActivate
    myPlayer.gameObject.SetActive(true);

    //or Instnatiate a new
    Instantiate(myPlayer);
}
endrik exe
  • 606
  • 3
  • 10
  • Hmmm...I could try this. Do I need to make a "myPlayer" variable or is that one part of Unity? – Cepheron Kalle Nov 11 '17 at 18:44
  • You have to make myPlayer variable and reference your player object to that field in the inspector. myPlayer object is correspond to this variable in OnTriggerEnter other.gameObject.SetActive(false); – endrik exe Nov 12 '17 at 01:49