Question is off-topic or not relevant
how to disable dontdestroyonload on a specific scene
hey, i'm making a 2d game that contains bonus scenes, so i manage to put my score to go over the scenes and don't destroy, so it would have the same value when the player went back to the main scene. here's the problem: when the player dies, the score does not reset. how can it make it reset when the player is dead? obs: the game over menu has a restart button and i tried to add something to his script, but it didn't work. the restart button script: public class LoadSceneOnClick : MonoBehaviour {
public void LoadByIndex(int sceneIndex)
{
SceneManager.LoadScene (sceneIndex);
}
}
the dontdestroyonload script:
public class SaveScore : MonoBehaviour {
void Awake ()
{
DontDestroyOnLoad (this);
if (FindObjectsOfType (GetType ()).Length > 1)
{
Destroy (gameObject);
}
}
}
thanks in advance c:
Answer by Dragate · Oct 24, 2017 at 08:39 AM
When your player dies, get reference to your score (via script) and destroy it. Or set a bool when the player dies and everytime you load another scene check that bool to determine if your score should be destroyed first.
public PlayerController pc;
void Awake () { pc = FindObjectOfType(); }
void Update () { if (pc.isDead) { Destroy (GameObject.CompareTag "SaveScore")); } }
something like that? ps: i would put the savescore tag on my canvas that holds my score count, but where should i put this code? on my player script and make a reference to savescore script or create a new one?
Answer by Cuttlas-U · Oct 24, 2017 at 08:48 AM
u should use PlayerPrefs to get data in different scene; the way u are doing makes evey thing complicated;
i'm using it for my high score system, but i don't know how to use it with game objects going through scenes, can you help me?