GameObjects in the public static dictionary <String, GameObject> are destroyed when the scene changes in Unity
The first formal announcements: Programming language: C # in Unity (MonoBehavior) My skill level: Kinda noob (less than six months of C # experience).
I make a block breaker (Arkanoid) and create a control system. The game is a one-time game, everything is deleted when the web assembly is closed (without cache, without files, without serializable classes).
The Acheivement system contains: Acheivement class: many variables and several methods for tracking progress. The script is attached to an empty GameObject with the same name. Normal class.
Class "AManager". A class that initializes a bunch of copies of the "Acheivement" class gives them individual values and adds them to the dictionary mentioned below. Class Singelton.
Class "AcheivementDictionary": contains only a private static dictionary and access methods. Also class singelton
Problem:
Achievements are improved and added to the "beginning" of the first scene. After they are added, I retrieve the GameObjects and check if their tag works, and everything is fine. Then, from my levelManager class, I call the same method from the next scene and get a message about the destruction of gameObjects. If I run them, they will also return null.
So ... my real question is:
How do I make my instantiated copies of GameEbjects to continue all scene changes in Unity?
Code References:
I don’t think that “acheivement” is a class that has any problem, if you need code from this class, just scream.
"AManager"
public class Amanager : MonoBehaviour {
public static Amanager acheivementManager = null;
public GameObject myAchObject;
void Awake () {
if (acheivementManager != null) {
Destroy(gameObject);
} else {
acheivementManager = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
void Start () {
myAchObject.AddComponent<Acheivement>();
initAcheivements ("completeLevelThree", "Level 3", "Complete level 3","Level", 3,5);
initAcheivements ("completeLevelOne", "Level 1", "Complete level 1","Level", 1,5);
}
private void initAcheivements(string inAch, string inNavn, string inHow, string inCountType, int goalVal, int Reward) {
Vector3 loc = new Vector3 (0f, 0f,0f);
GameObject testAch;
testAch = Instantiate(myAchObject, loc, Quaternion.identity) as GameObject;
//testLives.SetActive (true);
//testLives.gameObject.tag = "clone";
testAch.GetComponent<Acheivement>().setName(inNavn);
testAch.GetComponent<Acheivement>().setHowToGet(inHow);
testAch.GetComponent<Acheivement>().setCountType(inCountType);
testAch.GetComponent<Acheivement>().setGoalVal(goalVal);
testAch.GetComponent<Acheivement>().setReward(Reward);
testAch.tag = inCountType;
Debug.Log ("InitiAch did run");
AcheivementDictionary._Instance.achRegAdder(inNavn,testAch);
}
AchewiveDictionary Code:
public class AcheivementDictionary : MonoBehaviour {
public static AcheivementDictionary _Instance;
private static Dictionary<string,GameObject> AchReg = new Dictionary<string,GameObject>();
void Awake () {
if (_Instance != null) {
Destroy(gameObject);
} else {
_Instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
public void achRegAdder (string inName, GameObject theAch) {
AchReg.Add(inName,theAch);
Debug.Log ("achRegAdded did run");
readThisString = inName;
tryRead = true;
}
private void readIt() {
Debug.Log ("readItRan");
GameObject testShit;
AchReg.TryGetValue("Level 1",out testShit);
Debug.Log("if it works level should stand there ---> " + testShit.tag);
tryRead = false;
}
public void achRegReader () {
readIt ();
}
:
initAch
achRegAdded
initAch
achRegAdded
readItRan
, --- >
readitRan
MissingReferenceException: "GameObject" , . script , , obejct.
( : Debug.Log( " , --- > " + testShit.tag);
, achRegReader, ( ): AcheivementDictionary._Instance.achRegReader();
: , 2. , . , .
:
, Singelton. Acheivements , dontDestroyOnLoad, , . , 6 , , !