Trouble with Saving Vars across Scenes! I must be insane!

All,

I have read and study many of the forum threads regarding retaining “globals” across scenes… But I am really having trouble… Obviously I do not understand…

HELP!

In the main scene the user selects a tile from the UI and I want to save the Texture from this tile globally.

I have created a game object in the main scene called “GlobalVars” to which I have attached a “MainVars” C# script as follows:


using UnityEngine;
using System.Collections;

public class MainVars : MonoBehaviour
{
private Texture2D theTexture;

// Use this for initialization
void Awake ()
{
// Preserve this Class… This is just a container class for our global vars…
DontDestroyOnLoad(this);
}

public void RememberTexture(Texture2D LastTexture)
{
theTexture = LastTexture;
if (theTexture != null)
{
Debug.Log(“Texture Set NON-NULL in MainVars Class…\n”);
}
else
{
Debug.Log(“Texture WAS SET TO NULL!! in MainVars Class…\n”);
}
}

public void ClearTexture()
{
theTexture = null;
}

public Texture2D GetTileTexture()
{
if (theTexture == null)
{
Debug.Log(“Texture was NULL on GetTileTexture…\n”);
}
else
{
Debug.Log(“Texture Valid on GetTileTexture…\n”);
}
return(theTexture);
}
}


Now in the main scene I call the RememberTexture method and debug.log reports correctly that the passed parameter is NOT NULL…

Then later I use Application.LoadLevel to load the next level…

In the scripts for the new level I use GameObject,Find(“/GlobalVars”) to get the presistent GO. Next I use GO.GetComponent(“MainVars”) to retrieve the script… and finally call GetTileTexture…

BUT debug.log reports that the texture IS NULL at this point…

I have also tried:

  1. Setting the Class Vars as static…
  2. Using DontDestroyOnLoad(gameObject) as well.
  3. Using Start method to init instead of Awake.
  4. Killing Chickens and sprinkling blood around…

What is going on? Can anyone shed some light on the issue? I am nearly ready to book a room at the mental hospital!

Thanks in advance,

caiken

An update to the problem…

Simply types work… BUT NOT the Texture…

If I add “private string theString;” to the class definition and set/retrieve it using the same method, IT WORKS!

Why not a Texture2D? Is this really a pointer? and the underlying storage is being free’d during the unload of the previous scene?

Thanks again for any help.

caiken