- Home /
Question by
Sayden96 · Aug 03, 2021 at 09:07 AM ·
scripting problemsceneunity 2dscene-loading
Load Scene Bug
When I load the scene for my principal menu in my game the life counter dosen't drop if I dead in the scene, only drops one time and nothing more and blocks every time. If i play the scene directly my life counter work perfectly and i don't have problems. I dosen't know what happens for load to the menu.
The script of the menu:
public class LevelsMenu : MonoBehaviour
{
public void Level1()
{
SceneManager.LoadScene("LevelTutorial");
Destroy(GameObject.Find("GameMasterC"));
Destroy(GameObject.Find("CanvasPermanent"));
Cursor.visible = false;
}
public void Level2()
{
SceneManager.LoadScene("Level2");
Destroy(GameObject.Find("GameMasterC"));
Destroy(GameObject.Find("CanvasPermanent"));
Cursor.visible = false;
}
public void Level3()
{
SceneManager.LoadScene("Level3");
Destroy(GameObject.Find("GameMasterC"));
Destroy(GameObject.Find("CanvasPermanent"));
Cursor.visible = false;
}
public void Level4()
{
SceneManager.LoadScene("Level4");
Destroy(GameObject.Find("GameMasterC"));
Destroy(GameObject.Find("CanvasPermanent"));
Cursor.visible = false;
}
public void Level5()
{
SceneManager.LoadScene("Level5");
Destroy(GameObject.Find("GameMasterC"));
Destroy(GameObject.Find("CanvasPermanent"));
Cursor.visible = false;
}
The script of LifeManager
public class LifeManager : MonoBehaviour
{
public int startingLives;
private int lifeCounter;
private Text theText;
public GameObject gameOverScreen;
public PlayerController2D Player;
public float waitAfterGameOver;
// Start is called before the first frame update
void Start()
{
theText = GetComponent<Text>();
lifeCounter = startingLives;
Player = FindObjectOfType<PlayerController2D>();
}
// Update is called once per frame
void Update()
{
if(lifeCounter == 0)
{
gameOverScreen.SetActive(true);
Destroy(GameObject.Find("Canvas"));
}
theText.text = "x " + lifeCounter;
/*
if(gameOverScreen.activeSelf)
{
waitAfterGameOver -= Time.deltaTime;
}
if(waitAfterGameOver < 0)
{
SceneManager.LoadScene("Menu");
Destroy(GameObject.Find("CanvasPermanent"));
}*/
}
public void GiveLife()
{
lifeCounter++;
}
public void TakeLife()
{
lifeCounter--;
Debug.Log("Istakenlife");
}
}
The script of the CanvasPermanent:
public class CanvasPermanent : MonoBehaviour
{
private static CanvasPermanent Instance;
void Awake()
{
if (Instance)
{
Destroy(this.gameObject);
Debug.Log("IsDestroyt");
return;
}
Instance = this;
DontDestroyOnLoad(this.gameObject);
Debug.Log("Isnotdestroyed");
}
}
Comment