Problem using DontDestroyOnLoad method

Hi I need to navigate between two scenes. To reserve the variables and other information like player details I am using DontDestroyOnLoad(gameObject) method but it is showing error as I move to next scene. It is showing a NullReferenceException error. Please help. I tried DontDestroyOnLoad(this) also.

Don’t use this function on objects in a scene that you want to load multiple times. This would cause that the second time you load the scene you’ll end up with two instances of this object.

Also it’s important that you use DontDestroyOnLoad on the top most object. Using it on a child object won’t do anything. Also keep in mind that the whole gameobject will survive a scene load along with all child objects.

You are right but in my case I have written the code in Awake function. I am passing the gameObject in the function but is is showing error.

I searched for my answer but every where I found that I should use this function to retain my vaues but unfortunately I am not able to use this function.