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!

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

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?

I found the solution.