Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sericet · Jan 06, 2012 at 09:14 PM · guitextures

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Sericet · Jan 07, 2012 at 10:43 PM

I found the solution.

http://answers.unity3d.com/questions/122479/attack-damage-scrolling.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sericet · Jan 06, 2012 at 11:14 PM 0
Share

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

avatar image Sericet · Jan 07, 2012 at 01:36 PM 0
Share

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

avatar image Sericet · Jan 07, 2012 at 03:54 PM 0
Share

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)

avatar image Anxo · Jan 07, 2012 at 05:06 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How will i get animated gif images in scene? 6 Answers

Resizable GUI textures 1 Answer

GUILayout: Placing images on top of each other 3 Answers

How do I create GUI Objective Textures based on Player Position? 1 Answer

Easy way to center GUI Textures on a path? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges