There is a method for reload a level but not all objects? because I want reload a level but I don’t want to reload the coins that the player had taken and the score…
You simply need to preserve the required information, load the whole level, apply the information and that’s it. Could you explain your use case a little more detailed?
Probably, you need something like DontDestroyOnLoad() method - Unity - Scripting API: Object.DontDestroyOnLoad
But as Dantus said, it depends on details of your case.
The player take the coins into the level but if he dead the level must be reloaded (because many variabile must be resetting) but not the coins that he had taken (they not must be dislayed), only the coins that the player must be take yet
Use Application.LoadLevel(Level num / name here)
No, it reload all, also the coins ! I want that the coins taken not reappare into the scene when the level is reloaded !
There is no simple way… Preserve the data you need, load the whole level and then take the preserved data and apply it. E.g. remove the already collected coins and what not.
The easiest (also unrecommended) thing to do is make your score a static variable. The static keyword means that only a single score variable will exist in your entire program, so it won’t be lost between levels.
Another way is to store the information in PlayerPrefs, then load it when you reload the scene.
DontDestroyOnLoad() is suggested. This allows you to keep the same object between scenes. However, if you just reload the same scene then you will end up with duplicate objects.
public int score { get; set; }
void Awake()
{
if (initialized)
Destroy(gameObject);
else
{
GameObject.DontDestroyOnLoad(gameObject);
initialized = true;
}
}
static bool initialized { get; set; }
You can see that I check to see if we are initialized, then destroy the duplicate if we have already set one to dont-destroy-on-load. You might also want to look at the Singleton pattern.
The way I deal with these duplication objects is to make scene 0 our Initialization scene, initialize our score manager there, set it to dont-destroy-on-load, then scene 1 will be our gameplay scene. This way we can reload scene 1 without duplicating the dont-destroy-on-load objects from scene 0, our intialization scene.
Using DontDestroyOnLoad is a dead end for this - it’s always just going to give you duplicate coins, since you’re placing the coins in the level which is being reloaded.
My advice: You basically need to store the state of each coin, and to do that, you’re gonna have to set up a system that assigns each coin an ID. On start, either the coin or a manager will check the coin against the corresponding stored state, and disable/destroy the ones that have been collected.
This is kind of a complex idea to get across (doubly so if you’re not familiar with static members, dictionaries, and generic classes), so I wrote some quick code for this. Something like this should work (though it is untested):
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
public class Coin : MonoBehaviour {
private static int IDCounter = 0;
public int thisCoinID = -1;
void Reset() { // after you first add this script, run Reset on every coin; when creating new coins it should assign new numbers as you create them
thisCoinID = IDCounter;
IDCounter++;
}
public static Dictionary<int, bool> coinCollectedDatabase;
void Awake () {
if (coinCollectedDatabase == null) coinCollectedDatabase = new Dictionary<int, bool>();
}
void Start () {
if (coinCollectedDatabase.ContainsKey(thisCoinID) ) { //we're reloading the scene
if (coinCollectedDatabase[thisCoinID]) Destroy(gameObject); //we've been collected already; destroy ourself
}
else {
coinCollectedDatabase.Add(thisCoinID, false);
}
}
void CallThisWhenCollected() {
coinCollectedDatabase[thisCoinID] = true;
}
}
StarManta has good advice. I totally didn’t see the part about not reloading coins the player already took.
Generally things like this happen in two ways.
- Load everything, destroy what shouldn’t be there.
- Load nothing, then decide what to instantiate to fill the level.
Either way you will have to give each coin it’s own unique ID and track it. StarManta’s using a static Dictionary so that this data is preserved through scene changes.
Thank you everybody for the answers ! I think that I will use SantaManta’s solution