- Home /
Why doesn't my texture show up?
Hello,
I'm trying to make an image show up when the player clicks on one of the objects in my game. However, the code doesn't seem to be working. Can someone experienced help me?
Thank you so much!
Here's the code:
bool showPaw=false;
public Texture2D catPaw;
void OnMouseDown () {
showPaw = true;
Destroy(this.gameObject);
}
void OnGUI (){
if (showPaw == true)
GUI.DrawTexture(new Rect(0,0, 100,100),catPaw,ScaleMode.ScaleToFit);
Debug.Log("CAT PAW!!");
}
}
Is it reaching the log and not showing the image?
Where are you setting showPaw to false? Perhaps a bit more of code would help to find the issue.
You are destroying the game object which is responsible for showing the GUI image. Dont destroy the game object.
I've worked my way around the issue through your advice, TRG96. Thank you! And thank you too for your response nicolasjr!
Answer by eatyourpotato · Apr 23, 2015 at 01:35 PM
Thanks for your answers, but I've fixed the problem. I wrote a separate script so that destroying the game object won't affect my image showing up. I've made the image a sprite instead of a texture2D. Here's the code for those interested:
private SpriteRenderer spriteRenderer;
public Sprite[] catPaw;
void Start(){
spriteRenderer = GetComponent<SpriteRenderer> ();
spriteRenderer.enabled = false; //makes sure that the image won't show up at first
}
void FixedUpdate(){
if (destroyMouse.showPaw == true) {
spriteRenderer.enabled = true; //makes the image show up
}
else {
spriteRenderer.enabled = false; //when "showPaw==false", make the image disappear again
}
GetComponent<SpriteRenderer>().sprite = catPaw[0];
}
Your answer
Follow this Question
Related Questions
How to get correct zooming in GUI.DrawTexture? 1 Answer
GUI DrawTexture is sliding across the screen? 0 Answers
Another GUI question. HealthBar 1 Answer
DrawTexture on RenderTexture without Camera 1 Answer
Texture on center bottom of screen 3 Answers