Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Reiniciar un nivel.

Iniciado por ramunda, Septiembre 01, 2016, 05:20:36 PM

Tema anterior - Siguiente tema
Septiembre 01, 2016, 05:20:36 PM Ultima modificación: Septiembre 01, 2016, 08:24:16 PM por ramunda
Buenas tardes, tengo un pequeño y espero decir pequeño porque seria una ***a...problemilla.
 
Tengo un proyecto de juego que solo tiene una escena.
 
Bien si la palmas te sale el típico botón reiniciar.
 
bien el juego empieza de nuevo pero....algunos scripts o componentes no reaccionan / funcionan adecuadamente como si se cargaran mal.Estoy usando el ultimo Unity y creo que he probado todas las maneras de reiniciar el nivel:
 

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class gameover : MonoBehaviour {
 

public void reiniciar()
{
//SceneManager.LoadScene("mesapinball"); ////ESTA NO VA BIEN
//   Application.LoadLevel (Application.loadedLevelName);//ESTA TAMPOCO....
SceneManager.LoadScene(SceneManager.GetActiveScene().name);///TAMPOCO
Time.timeScale = 1.0F;
}
   
public void salir()
{
Application.Quit();
}
}

 
todas lo reinician pero omitiendo trozos....debería meter un dontdestroy a todo lo que no funciona?
 
 
 
Sabéis de otra manera para que se cargue todo?
 
EDITO;
 
el que no se carga de nuevo al hacer el restart es peculiarmente este:
 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class droptargetss : MonoBehaviour {
public float dropDistance = 1.0f;
public int bankID = ;
public float resetDelay = 0.5f;
public static List<droptargetss> dropTargets = new List<droptargetss>();
public int targetValue = 100;
public int bankValue = 10000;
AudioSource audio;
public AudioClip sonidotarget;   
public AudioClip cantina;   
public bool isDropped = false;
public GameObject luzx1;
public GameObject luzx2;
public  GameObject TextOnSpotGO;
//public GameObject cantinavideo;
 
//   void Awake() {
//      DontDestroyOnLoad(transform.gameObject);
//   }
// Use this for initialization
void Awake ()
{
   dropTargets.Add(this);
   audio = GetComponent<AudioSource>();
   luzx2.SetActive (false);
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter()
{
   if(!isDropped)
   {
      //Drop Target
      transform.position += Vector3.down*dropDistance;
      isDropped = true;
      ScoreManager.Instance.ScoreUp(targetValue);
      GameObject textInstance = Instantiate (TextOnSpotGO, transform.position, Quaternion.identity) as GameObject;
      textInstance.GetComponent<TextOnSpotScript>().DisplayText = targetValue * ScoreManager.Instance.scorePower + "";
      //         ScoreManager.Instance.ScoreUp(targetValue);
      //         //ScoreManager.Instance.ScoreUp(scoreValue);
      //         Instantiate (TextOnSpotGO, transform.position, Quaternion.identity);
      //         TextOnSpotGO.GetComponent<TextOnSpotScript>().DisplayText = ScoreManager.Instance.scoretarjets * ScoreManager.Instance.scorePower + "";
      audio.PlayOneShot(sonidotarget, 0.7F);
 
      //Check to see if the rest of the bank has been dropped
      bool resetBank = true;
      foreach(droptargetss target in dropTargets)
      {
         if(target.bankID == bankID)
         {
            if(!target.isDropped)
            {
               resetBank = false;
            }
         }
      }
      //Reset all drop targets in bank if the bank has been dropped
      if(resetBank)
      {
         //ScoreManager.Instance.score += bankValue;
         ScoreManager.Instance.ScoreUp(bankValue);///(scoreValue);
         Instantiate (TextOnSpotGO, transform.position, Quaternion.identity);
         TextOnSpotGO.GetComponent<TextOnSpotScript>().DisplayText = ScoreManager.Instance.bankvalue * ScoreManager.Instance.scorePower + "";
         Invoke("ResetBank", resetDelay);
         ScoreManager.Instance.SetScorePower (2);
         audio.PlayOneShot(cantina, 0.7F);
         //cantinavideo.SetActive (true);
         luzx2.SetActive (true);
         luzx1.SetActive (false);
      }
   }
}
void ResetBank()
{
   foreach(droptargetss target in dropTargets)
   {
      if(target.bankID == bankID)
      {
         target.transform.position += Vector3.up*dropDistance;
         target.isDropped = false;
      }
   }
}
}

 
Alguna idea?
 
He probado con dont destroy pero al reiniciar el nivel se duplica...
 
 
 
 
 
 
 
 
 
 

Septiembre 01, 2016, 08:24:23 PM #1 Ultima modificación: Septiembre 01, 2016, 08:27:18 PM por elanguido
Cualquiera de esas formas vale para reiniciar el nivel y, que yo sepa, no hay otra manera. 
 
Eso que comentas de que se omiten trozos es porque algo impide que la escena se destruya correctamente. Evita al máximo los objetos con DontDestroyOnLoad, las variables estáticas etc. si por algún motivo de peso necesitas tener algo de esto, revisa que no tengas en ellos referencias a otros objetos de la escena en el momento de reiniciar el nivel. El problema está ahí casi seguro.

elamguido, gracias por la respuesta , no tengo ningun dontdestroy ni variables estaticas...
 
Es muy raro, pero raro raro, cuando empieza el juego todo va bien,luego al resetear el nivel solo este objeto funciona parcialmente.....
 
 

Cita de: ramunda date=1472766329elamguido, gracias por la respuesta , no tengo ningun dontdestroy ni variables estaticas...
   
   
      Es muy raro, pero raro raro, cuando empieza el juego todo va bien,luego al resetear el nivel solo este objeto funciona parcialmente.....
   
   
       
   


Esto parece un singleton. Se destruye correctamente?
 

ScoreManager.Instance

Septiembre 02, 2016, 12:26:17 AM #4 Ultima modificación: Septiembre 02, 2016, 12:42:36 AM por ramunda
iRobb gracias por la respuesta , si eso es un singleton...puede ser de alli?
 
y como se si se destruie?
 
provare a quitarle el singleton....aver si me lo cargo todo XD
 
Val he quitado el
 
singleton y puesto las vriables estaticas para poder acceder a ellas...
 

public   static int score =;
public   static int scorePower = 2;
//public   int bankvalue =10000;
public    TextMesh display;
public GameObject Ballmanager;
private bool detener;
private bool extra;
private int nextExtraBall = ;
public int scoreExtraBall = 10000;
public GameObject Extraball;
public AudioClip extrabolaaudio;
AudioSource audio;

 
mismo resultado
 
al reiniciar la escena funciona solo parcialmente (
 
 

Además de DontDestroyOnLoad y objetos estáticos, tienes que tener en cuenta las corrutinas y las invocaciones.
 
En el script aparece un Invoke. Prueba a añadir dentro de la función-evento OnDestroy() el método CancelInvoke() para cancelar todas las invocaciones, y resetea componentes y variables a 0 si es necesario.

Septiembre 02, 2016, 01:52:03 PM #6 Ultima modificación: Septiembre 02, 2016, 04:30:59 PM por ramunda
Que frustamiento...nada funciona,he provado a ponerle un debug log para ver si llegaba a hacer el bank y si lo hace , pero no envia el ,mensaje mas alla osea ni apaga ni enciende las luces ni aumenta el porcentaje.Incluso la bola extra funciona simpre que tembien esta en este script...
 
tiene que ser algo de este script:
 
por favor echadle un ojo...no se que mas hacer :(
 
Este el el que controla los puntos y los x2 y x3:
 
 
 

using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance;
void Awake()
{
Instance = this;
}
public   int score =;
public   int scorePower = 2;
public   int bankvalue =10000;
public    TextMesh display;
public GameObject Ballmanager;
private bool detener;
private bool extra;
private int nextExtraBall = ;
public int scoreExtraBall = 10000;
public GameObject Extraball;
public AudioClip extrabolaaudio;
AudioSource audio;
 
void Start()
{
   nextExtraBall += scoreExtraBall;
   audio = GetComponent<AudioSource>();
}
 
void Update()
{
   if(extra){
      Extraball.SetActive (true);
   }
   if (display)
   {
      display.text = score.ToString ("D8");
   }
   if (score >= nextExtraBall && detener == false) {
   millonn();
   nextExtraBall += scoreExtraBall;
  // extra = true;
   Extraball.SetActive(true);
   audio.PlayOneShot(extrabolaaudio, 0.7F);
   Invoke ("apaga", 4);
}   
}
public   void ScoreUp(int points)
{
score += (points * scorePower);
}
public  void SetScorePower(int power)
{
   scorePower = power;
}
void millonn()
{
Debug.Log ("MILLLOOOOONNN");
Ballmanager.GetComponent<BallManager> ().lives += 1;
}
void apaga()
{
   //extra = false;
   Extraball.SetActive(false);
}
}
 

 
y estos los que dan problemas :
 
1:
 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class letrasderecha : MonoBehaviour {

   public float dropDistance = 1.0f;
   public int bankID = ;
   public float resetDelay = 0.5f;
   public static List<letrasderecha> dropTargets = new List<letrasderecha>();
   public int targetValue = 100;
   public int bankValue = 10000;
   AudioSource audio;
   public AudioClip sonidotarget;   
   public bool isDropped = false;
   public  GameObject TextOnSpotGO;
    public AudioClip duel;
    public GameObject luzx1;
    public GameObject luzx2;
    public GameObject luzx3;
    public int cantidadx =;
   
   void Start ()
   {
      dropTargets.Add(this);
      audio = GetComponent<AudioSource>();
       luzx3.SetActive (false);
   }
         
   void OnCollisionEnter()
   {
      if(!isDropped)
      {
         //Drop Target
         transform.position += Vector3.down*dropDistance;
         isDropped = true;
         ScoreManager.Instance.ScoreUp(targetValue);
         GameObject textInstance = Instantiate (TextOnSpotGO, transform.position, Quaternion.identity) as GameObject;
         textInstance.GetComponent<TextOnSpotScript>().DisplayText = targetValue * ScoreManager.Instance.scorePower + "";
          audio.PlayOneShot(sonidotarget, 0.7F);
         //Check to see if the rest of the bank has been dropped
         bool resetBank = true;
         foreach(letrasderecha target in dropTargets)
         {
            if(target.bankID == bankID)
            {
               if(!target.isDropped)
               {
                  resetBank = false;
               }
            }
         }
         //Reset all drop targets in bank if the bank has been dropped
         if(resetBank)
         {
            //ScoreManager.Instance.score += bankValue;
             Debug.Log("TODOS ABAJO");
            ScoreManager.Instance.ScoreUp(bankValue);///(scoreValue);
            Instantiate (TextOnSpotGO, transform.position, Quaternion.identity);
            //TextOnSpotGO.GetComponent<TextOnSpotScript>().DisplayText = ScoreManager.Instance.bankvalue * ScoreManager.Instance.scorePower + "";
             textInstance.GetComponent<TextOnSpotScript>().DisplayText = bankValue * ScoreManager.Instance.scorePower + "";
            Invoke("ResetBank", resetDelay);
             audio.PlayOneShot(duel, 0.7F);
             ScoreManager.Instance.SetScorePower (cantidadx);
             luzx3.SetActive (true);
             luzx2.SetActive (false);
             luzx1.SetActive (false);
         }
      }
   }
   void ResetBank()
   {
      foreach(letrasderecha target in dropTargets)
      {
         if(target.bankID == bankID)
         {
            target.transform.position += Vector3.up*dropDistance;
            target.isDropped = false;
         }
      }
   }
}

 
2:
 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class letras : MonoBehaviour {
public float dropDistance = 1.0f;
public int bankID = ;
public float resetDelay = 0.5f;
public static List<letras> dropTargets2 = new List<letras>();
public int targetValue = 100;
public int bankValue = 10000;
AudioSource audio;
public AudioClip sonidotarget;   
public bool isDropped = false;
public  GameObject TextOnSpotGO;
public AudioClip duel;
public GameObject luzx1;
public GameObject luzx2;
public GameObject luzx3;
public int cantidadx =;
 
void Start ()
{
   dropTargets2.Add(this);
   audio = GetComponent<AudioSource>();
   luzx3.SetActive (false);
}
 
void OnCollisionEnter()
{
   if(!isDropped)
   {
      //Drop Target
      transform.position += Vector3.down*dropDistance;
      isDropped = true;
      ScoreManager.Instance.ScoreUp(targetValue);
      GameObject textInstance = Instantiate (TextOnSpotGO, transform.position, Quaternion.identity) as GameObject;
      textInstance.GetComponent<TextOnSpotScript>().DisplayText = targetValue * ScoreManager.Instance.scorePower + "";
      audio.PlayOneShot(sonidotarget, 0.7F);
      //Check to see if the rest of the bank has been dropped
      bool resetBank = true;
      foreach(letras target in dropTargets2)
      {
         if(target.bankID == bankID)
         {
            if(!target.isDropped)
            {
               resetBank = false;
            }
         }
      }
 
      if(resetBank)
      {
         //ScoreManager.Instance.score += bankValue;
         Debug.Log("TODOS ABAJO");
         ScoreManager.Instance.ScoreUp(bankValue);///(scoreValue);
         Instantiate (TextOnSpotGO, transform.position, Quaternion.identity);
         //TextOnSpotGO.GetComponent<TextOnSpotScript>().DisplayText = ScoreManager.Instance.bankvalue * ScoreManager.Instance.scorePower + "";
         textInstance.GetComponent<TextOnSpotScript>().DisplayText = bankValue * ScoreManager.Instance.scorePower + "";
         Invoke("ResetBank", resetDelay);
         audio.PlayOneShot(duel, 0.7F);
         ScoreManager.Instance.SetScorePower (cantidadx);
         luzx3.SetActive (false);
         luzx2.SetActive (true);
         luzx1.SetActive (false);
      }
   }
}
void ResetBank()
{
   foreach(letras target in dropTargets2)
   {
      if(target.bankID == bankID)
      {
         target.transform.position += Vector3.up*dropDistance;
         target.isDropped = false;
      }
   }
}
}

 
He añadido esto:
 

using UnityEngine;
using System.Collections;
public class dontdestroy : MonoBehaviour {
void Start()
{
   if(GameObject.Find(this.gameObject.name) && GameObject.Find(this.gameObject.name) != this.gameObject )
   
   {
      Destroy(gameObject);
    }
else
   {
   DontDestroyOnLoad(this.gameObject);
   }
}
 
}

 
para asegurarme de que se destruye...pero sigue igual.Igual ,e refiero que al empezar ba de lujo hasta que se acaban las vidas y se resetea luego ya no funciona.
 
Bueno algo nuevo, si solo pongo un script de los dos funciona bien en este caso letrasderecha...se interferirán unos con otros?
 
 

A ver, haz la siguiente prueba:
 
crea una nueva escena que sólo tenga la MainCamera. Cuando de llegué el momento de reiniciar tu nivel, en vez de hacerlo, carga esa escena. una vez cargada, mira en el inspector a ver qué hay ahí.

Etiquetas: