- Home /
DontDestroyOnLoad not working in Unity 4.3
I just upgraded Unity3D from 4.1 to 4.3. I have a working project, and it doesnt work after the upgrade. The reason is a gameobject seems to be destroyed after I change the scene. Here is some codes:
public class GameDataObject:Object {
public string name;
}
public class GameMainScript:MonoBehaviour {
public static GameMainScript mainScript;
public GameDataObject gameDataObject;
void Start() {
gameSataObject = new GameDataObject();
gameDataObject.name = "ABC";
DontDestroyOnLoad(gameDataObject);
Application.LoadLevel("NewScene");
}
}
In NewScene, I have a script:
Debug.Log(GameMainScript.mainScript.gameDataObject.name);
It shows "ABC" in Unity 4.1, but now in Unity 4.3, it shows "".
Any idea? Thanks
sorry, should be my typo here, the original code is .name
I haven't used the Object baseClass directly or done much w/ statics, but - is the GameDataObject (top thing) destroyed on going to NewScene? If it's destroyed, the pointers from Game$$anonymous$$ainScript to gameDataObject might be too
It seems the problem comes from gameDataObject, I immediately Debug.Log(gameDataObject)
after gameDataObject = new GameDataObject();
, it shows null
, any idea?
oh wait, Debug.Log(gameDataObject.name);
shows "ABC" before LoadLevel
, but shows "" after LoadLevel
, I guess it's still related to DontDestroyOnLoad
. But why? It works in Unity 4.1 and Unity 3 before
Answer by fanling3 · Feb 18, 2014 at 07:57 AM
Finally I made it works by changing
gameDataObject = new GameDataObject();
into
gameDataObject = ScriptableObject.CreateInstance<GameDataObject>();
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad() does not seem to be working. 3 Answers
FindGameObjectsWithTag not finding a tagged object 4 Answers
DontDestroyOnLoad duplication. 0 Answers
Next LEVEL Load Problem. 2 Answers
Setting public GameObject to a different Prefab through code 0 Answers