- Home /
Activating a GUITexture by clicking on an object (script in c#)?
Hi everyone,
I am new in coding and unity in general. I am working on a simulation, scripted in c#, and I want that the player is able to click on an object and get more information about that object in a popup. The popup must consist of a PNG(homemade (name: clickonme)) and a cross in a small box(generated by Unity) to exit the popup. In this picture (see attachments if this is too small to read)
you can see the errors that Unity indicates (MonoDevelop says that there are no errors), I have obviously done something wrong and I think that the thing that goes wrong is me not knowing how to tell Unity which file(the PNG) to use. (I really don't know how to formulate this better sorry)
So if someone knows how to correct this that would be great!
Thanks for replying and you can always ask me if I forgot some information!
This is my code:
using UnityEngine;
using System.Collections;
public class onclickGUI : MonoBehaviour {
// Use this for initialization
private bool PopUp;
public string Info;
public GUITexture clickonme;
void OnMouseDown()
{
PopUp = true;
}
void DrawInfo()
{
if(!clickonme)
{
Debug.LogError (clickonme);
return;
}
GUI.DrawTexture(Rect(50,50,250,250), clickonme, ScaleMode.ScaleToFit, true, 10.0f);
Rect close = new Rect (250,50,50,50);
if (PopUp)
{
GUI.Box(Rect, Info);
if (GUI.Button (close,"X"))
{
PopUp = false;
}
}
}
void OnGUI()
{
DrawInfo();
}
}
Answer by Landern · May 20, 2014 at 12:38 PM
Not sure how MonoDevelop didn't throw an error due to using a Rect type wrong in GUI.DrawTexture.
I don't know what size & position you wanted for the Info Box, so i made up some values in the new Rect. DrawTexture takes a Texture type, not a GUITexture, you were missing the new keyword on line:23.
Here you go, modify as needed:
using UnityEngine;
using System.Collections;
public class onclickGUI : MonoBehaviour {
// Use this for initialization
private bool PopUp;
public string Info;
public Texture clickonme;
void OnMouseDown()
{
PopUp = true;
}
void DrawInfo()
{
if(!clickonme)
{
Debug.LogError (clickonme);
return;
}
GUI.DrawTexture(new Rect(50,50,250,250), clickonme, ScaleMode.ScaleToFit, true, 10.0f);
Rect close = new Rect (250,50,50,50);
if (PopUp)
{
GUI.Box(new Rect(100, 50, 100, 50), Info);
if (GUI.Button (close,"X"))
{
PopUp = false;
}
}
}
void OnGUI()
{
DrawInfo();
}
}
Thank you for the quick reply! The errors are gone, but now the texture is always on and I want it to only show once you click on the object, however the "X" button is only appearing when you click on the object and disapearing when you click the "X" button. Do you know why the GUI.box is doing what I want and the GUI.DrawTexture not? Is the GUI.DrawTexture simply misplaced or is it something else? (if you think it's stupid of me to ask everything you can say that ;) )
Ow! Is it just that i forgot to put
if (PopUp)
above line 18?
Answer by Sisso · May 20, 2014 at 12:39 PM
You need to fix all this script errors indicated by unity3d before run your scene. For each error ready carefully, look the code, search in the internet, change and fix.
http://unity3d.com/learn/tutorials/modules/beginner/scripting
Your answer
Follow this Question
Related Questions
How to hide a part of a GUITexture that rotate ? 1 Answer
GUITexture is lagging my object 0 Answers
Multiple Cars not working 1 Answer
On Click GUI texture 0 Answers
Distribute terrain in zones 3 Answers