- Home /
Problem righted itself somehow so it is no longer relevant.
2D Texture Not Loading
Hey everyone,
Come across sort of a weird problem. I have a scene called "StageSelect" where you can choose which level you'll play. Stages you can select are shown in color and those you haven't unlocked yet are shown in black and white.
I have it set up so that when the scene loads, Unity checks the PlayerPrefs file for an integer (stage0XCheck). Stages that return a value of "0" are locked, "1" are unlocked, and "2~4" represent the various ranks that you can achieve. So the script accesses PlayerPrefs, grabs the int, and then chooses the 2D texture via a switch. The script is fairly long but this is what a piece of it looks like.
public Texture2D stage01img;
public Texture2D stage01rank;
public int stage01Check;
void Start()
{
StageCheck();
}
void StageCheck()
{
stage01Check = PlayerPrefs.GetInt("stage01");
//stage01
switch (stage01Check)
{
case 1:
stage01img = (Texture2D)Resources.Load("st1_select");
break;
case 4:
stage01img = (Texture2D)Resources.Load("st1_select");
stage01rank = (Texture2D)Resources.Load("st_select_grade_a");
break;
}
I have everything assigned public so I can see what's going on in the inspector and stage01rank isn't being assigned a texture. If I manually assign an image while the program is running, it appears immediately, otherwise no luck. I've checked the spelling of the file name and all of the images are located in the Resources folder. Debug.Log (and the loaded stage01img texture) are telling me that the program is entering the correct switch statement and running to completion.
Any ideas on what's wrong? I'd understand if it was a problem with my GUI settings but the image itself isn't even loading (as shown by the Stage 01rank = None(Texture2D) in the Inspector. :P
So weird. :P
I hate to ask a stupid question, but why don't you just do it by assigning textures in the editor?
Not stupid at all.
The reason I don't assign the texture in the editor is that when the scene loads, I have no idea what rank the player will have achieved. For example -
Player 1: Fires up the game for the first time, because he hasn't unlocked any of the stages, only the first stage icon will appear in color - the rest in black and white. Also, none of the "rank" textures (whether the player has earned a "gold", "silver", or "bronze" rating will be visible because he hasn't earned anything yet.)
Player 2: Has managed to unlock all of the stages except stage 5. When the scene loads, the program checks PlayerPrefs and sees what stages he's unlocked and what rank he's earned for each stage. Once the program has that data, it assigns the appropriate texture to the variable. stage01rank = (2DTexture) rankGold or stage01rank = (2DTexture) rankSilver, etc.
Since I have no idea what information will be present in the player's PlayerPrefs file, I don't know which images to assign in the editor.
Unless there is some other, super easy way that I'm overlooking;D
The problem sorted itself (somehow) so I'm closing this question.
Thanks for your time!
Follow this Question
Related Questions
GUI Style Issue-- Texture not changing 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
My OnGUI() Won't show the Button elements :( 0 Answers
cant get Texture2D.LoadImage() to work 0 Answers