Hola a todos, otra vez
Tengo este script que es el que controla todo el juego 2D
El caso es que cuando le doy a pausa o pierdo y me aparece la ui de gameover y le doy a reiniciar o a menu principal me sale este error
Tengo este script que es el que controla todo el juego 2D
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour
{
public static GameController gameController;
public GameObject Player;
[Header("Mecanica de Vidas")]
public Sprite spriteHeartFull;
public Sprite spriteHeartVacio;
public Image[] heart;
private int cantidadDeCorazones;
[Header("Sistema de Pausa")]
public GameObject gameObjectPause;
private bool pauseActive;
[Header("Sistema de Cuenta Regresivs")]
public float tiempo;
private float tiempoInicial;
public Text textTiempoRegresivo;
[Header("Victoria y Derrota")]
public GameObject ganar;
public GameObject gameOver;
private void Awake()
{
//Un sistema que no entiendo pero se usa para variables STATIC
DontDestroyOnLoad(this);
if(GameController.gameController == null)
{
GameController.gameController = this;
}
else if(GameController.gameController != null)
{
Destroy(gameObject);
}
}
private void Start()
{
//Compruebo si el objeto pausa esta activo y si es asi lo desactivo
if (gameObjectPause == true)
{
gameObjectPause.SetActive(false);
}
//La cantidad de corazones sera el tama�o del Arrays
cantidadDeCorazones = heart.Length - 1;
//La variable tiempoInicial sera el tiempo establecido para cada mapa
tiempoInicial = tiempo;
}
private void Update()
{
//Compruebo cuando preciono el boton escape para activar o desactivar la pausa
if (Input.GetKeyDown(KeyCode.Escape))
{
ActiveODesactivarPause();
}
//Inicio La cuenta regresiva
tiempo -= Time.deltaTime;
textTiempoRegresivo.text = tiempo.ToString("000");
//Si el tiempo ya va por la mita o mas abajo del tiempo incial la cuenta regresiva tendra un color rojo
if (tiempo <= (tiempoInicial / 2))
{
textTiempoRegresivo.color = Color.red;
}
}
public void QuitarUnaVida()
{
//Me aseguro de que cantidadDeCorzones nunca sea menor que 0 y que cuando llegue a ser 1 activar el gameOver
if (cantidadDeCorazones <= -1)
{
//Llamo al metodo ActivarGameOver
ActivarGameOver();
cantidadDeCorazones = 0;
}
//Quito una vida usando la cantidadDeCorazones para saber a cual corazon dirijirme y cambiarle el sprite
heart[cantidadDeCorazones].sprite = spriteHeartVacio;
//Le resto una a la cantidadDeCorzonse porque le quite una vida
cantidadDeCorazones--;
//Desvuelvo al jugador a su posicion incial
Player.transform.position = Player.GetComponent<PlayerController>().posicionIncial;
}
public void DarUnaVida()
{
//CantidadDeCorazones le sumo 1
cantidadDeCorazones++;
//Doy una vida usando la cantidadDeCorazones para saber a cual corazon dirijirme y cambiarle el sprite
heart[cantidadDeCorazones].sprite = spriteHeartFull;
//Me aseguro que cantidadDeCorazones nunca sea mayor que el heart.Length - 1(Es decir no tener mas corazones de los que poseo)
if (cantidadDeCorazones == heart.Length) cantidadDeCorazones = heart.Length - 1;
}
public void ActiveODesactivarPause()
{
//si apusa es activa la paso a desactiva y si es desactiva la paso a activa
pauseActive = !pauseActive;
//Activo o descativo el objeto pausa dependiendo del valor de pauseActive
gameObjectPause.SetActive(pauseActive);
//Detengo o prosigo el tiempo dependiendo de pauseActive
Time.timeScale = pauseActive ? 0 : 1;
}
private void ActivarGameOver()
{
//Activo el objeto gameOver
gameOver.SetActive(true);
//Paralizo el tiempo
Time.timeScale = 0;
}
}
El caso es que cuando le doy a pausa o pierdo y me aparece la ui de gameover y le doy a reiniciar o a menu principal me sale este error
MissingReferenceException: The object of type 'Text' has been destro*** but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.EventSystems.UIBehaviour.IsActive () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/UIBehaviour.cs:22)
UnityEngine.UI.Graphic.SetVerticesDirty () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:100)
UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:138)
GameController.Update () (at Assets/Scripts/GameController.cs:68)
[/quote]
Lo he buscado en el inspector y lo que ocurre es que se destruye el objeto text, pero al iniciar la partida vuelve a estar pero ya no funciona. Alguna idea=?
Ya esta echo
estoy intentando crear un script para recoger objetos al pulsar E pero al ponerlo en unity y darle a play no ocurre nada, sabrian decirme por que? aqui os dejo el script.