I’ve created a singleton of an ApplicationModel class to hold various info that will be used across scenes. I have created an array of Textures for possible different backgrounds that I would like to select at random. Problem I am having is when I select a Texture at random and save that to a variable in the singleton, I keep getting a null reference when I try to apply that texture to my background plane.
using UnityEngine;
using System.Collections;
public class ApplicationModel : MonoBehaviour
{
public static ApplicationModel appModel;
public Texture[] backgroundTextures;
public Texture _backgroundTexture;
void Start()
{
_backgroundTexture = backgroundTextures[Random.Range(0, backgroundTextures.Length)];
}
}
Then when I try to reference _backgroundTexture on my background plane, it gives me a null reference.
using UnityEngine;
using System.Collections;
public class BackgroundController : MonoBehaviour
{
void Start ()
{
//this.GetComponent<Renderer>().material.mainTexture = ApplicationModel.appModel._backgroundTexture; // DOES NOT WORK
this.GetComponent<Renderer>().material.mainTexture = ApplicationModel.appModel.backgroundTextures[Random.Range(0, ApplicationModel.appModel.backgroundTextures.Length)]; // WORKS
}
}
It works if I reference the Texture array with a random index, but does not work when I reference the Texture variable I set in Start().
How can I set up a variable inside my ApplicationModel to hold a single Texture from my Textures array that I can use throughout my scenes? I have gotten the above to work by creating a random index and instead of referencing the random Texture, I reference the Textures array at the random index. Is there a way to set the Texture variable that I can reference vs using an index?
Thank you.