- Home /
Show an Image after a button is pressed
Hi its been 8 and a half hours and i dont know what im doing wrong here. I have a picture called loading and i want to show it after play is pressed... then load the level. Im not getting any errors either. My picture is in the correct resources folder too. Here is the code. #pragma strict
var img : Texture2D;
img = Resources.Load("loadingpicture");
function OnGUI () {
if (GUI.Button (Rect (10,10,500,500), "I am a button")) {
img = Resources.Load("loadingpicture");
}
}
the "loadingpicture" never shows when i click the button. Can anyone please help?
I can't tell if it's my code or some other factors impacting the image being shown.
Answer by Rodiaz89 · Apr 07, 2014 at 12:21 AM
You are just loading the image in memory, you need to print the image in the screen through either GUI or a gameobject.
Here is an example: http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
So something like this would work if you want it through GUI
function OnGUI () {
if (GUI.Button (Rect (10,10,500,500), "I am a button")) {
GUI.DrawTexture(Rect(10,10,60,60), img);
}
}
It still doesn't work. it only works if i directly copy and paste the example code from the unity page. the code you provided doesn't work either. i completely agree with the code but the pic just doesn't want to show.
var img : Texture;
img = Resources.Load("loadingpicture");
function OnGUI () {
if (GUI.Button (Rect (10,10,100,100), "I am a button")) {
GUI.DrawTexture(Rect(10,10,500,500), img);
}
}
Hmmm, well make sure your image loaded is not null and its in the correct folder
you should have a Resources folder created, you can check by printing the img value to check if its null. Here is a post about that. http://answers.unity3d.com/questions/318921/correct-folder-for-resourcesload-.html Also try loading it in the Start or Awake functions just in case.
"it only works if i directly copy and paste the example code from the unity page..."
So why not do that?
Your answer
Follow this Question
Related Questions
initiate Imagebuttons in loop 1 Answer
iphone OnGUI define and test within rect?? 0 Answers
Image Sprite doesn't change with Button Onclick 0 Answers
[FIXED] Why is my GUI.Button always pressed? 0 Answers
Assiging an Image to a Button 1 Answer