- Home /
GUI Texture trouble
What I am trying to do.
I am trying to make my game display the points earned after shooting an enemey. I have a score counter but I want the points earned display over the object that was destroyed. Think modern warfare and the display you get for killing someone online.
I figured I could do this using GUI.Label and position it to the object that was destroyed. When the enemy is destroyed (I.E the mouse button is clicked and the raycast has hit a tag ="enemy". Display this texture.
Here is what I have
if (Physics.Raycast(ray, hit))
{
if(hit.transform.gameObject.tag == "Enemy"&&Input.GetMouseButton(0)) {
Destroy(hit.transform.gameObject);
SceneGUI.Score+=100;
PlaceLabel(hit,PointsGUI);//Calling the function below
}
} }
function PlaceLabel(hit:RaycastHit,PointsGUI:Texture2D)
{
GUI.Label (Rect (hit.transform.position.x, hit.transform.position.y, PointsGUI.width, PointsGUI.height),PointsGUI);
}
I am getting an error saying that you can only create GUI in the OnGUI function. OK! So I rewrote the code and put OnGUI() inside the if statement but another error(seems it doesn like the OnGUI inside an if statement.
So I am lost. I am not sure how to solve this problem. Maybe there is a better way to do this? I can display textures but not sure how to do it with an if statement. Any suggestions would be great!
Answer by Sericet · Jan 07, 2012 at 10:43 PM
I found the solution.
http://answers.unity3d.com/questions/122479/attack-damage-scrolling.html
Answer by dannyskim · Jan 06, 2012 at 10:05 PM
It's easier to use 3DText for your purpose if you're strictly only trying to display a string, integers, floats, etc...
http://unity3d.com/support/documentation/Components/class-TextMesh.html
Since it's a game object that is in world space, it would be much easier to programatically generate it through utilizing this. Of course, I can foresee you having a distance issue, in which case you may want to use GUIText or GUITexture, which are the older GUI elements that are not linked directly to the OnGUI method, making it easier for you to program and create them on the fly.
http://unity3d.com/support/documentation/ScriptReference/GUIText.html
http://unity3d.com/support/documentation/ScriptReference/GUITexture.html
To utilize these two GUI elements, you will most likely be using the Camera functions to translate the world position of the enemy that was hit / killed and translate that into screen coordinates utilizing
Camera.WorldToScreenPoint
http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToScreenPoint.html
Answer by Anxo · Jan 06, 2012 at 10:07 PM
An OnGUI function is seperate from something like Update or any other function. So you would have to place it outside of it.
private var hit : RaycastHit;
function OnGUI(){
GUI.Label (Rect (hit.transform.position.x, hit.transform.position.y, PointsGUI.width, PointsGUI.height),PointsGUI);
}
function Start(){
}
function Update(){
}
Looks like you might have some more problems with your code but you see what I mean?
Ok, I have been working on these suggestions and I am getting no where.
I am trying to make points appear when I shoot an object. The object is destroyed and the number 100 appears in that position and moves up a bit and fades out. I would like to put this inside an if statment that test to see if the object was clicked on. I have tryed GUIText, GUITextures, 3D text meshes. I'm sure someone has done this before but I have no clue where to begin. Thank you so much for your help in advance. (Getting upset lol)
It is very basic stuff. You should do your self a favor and do all of these $$anonymous$$odules first
http://www.unity3dstudent.com/category/modules/beginner/
When you are done, you will be able to do this type of stuff in no time.