x


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!

more ▼

asked Jan 06 '12 at 09:14 PM

Sericet gravatar image

Sericet
61 9 13 14

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest
more ▼

answered Jan 07 '12 at 10:43 PM

Sericet gravatar image

Sericet
61 9 13 14

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 06 '12 at 10:05 PM

dannyskim gravatar image

dannyskim
3.8k 5 7 19

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Jan 06 '12 at 10:07 PM

Anxowtf gravatar image

Anxowtf
1.6k 22 26 37

Yes, I am aware that the ongui must be separate.

Jan 06 '12 at 11:14 PM Sericet

Still not sure how to do this. Could someone help.

Jan 07 '12 at 01:36 PM Sericet

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)

Jan 07 '12 at 03:54 PM Sericet

It is very basic stuff. You should do your self a favor and do all of these Modules 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.

Jan 07 '12 at 05:06 PM Anxowtf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3673
x388

asked: Jan 06 '12 at 09:14 PM

Seen: 712 times

Last Updated: Jan 07 '12 at 10:43 PM