Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by sketchers1 · Sep 09, 2011 at 01:38 AM · guigameobjectobjectclick

GUI on click

So, I have made a map for my game. There are 36 different territories. Each Territory is a different game object. In this map scene, I want it so that when I click one of the territories, i have it so that information pops up on the side. Then when I click another, information pops up for that. I am almost entirely new to scripting. Is there a way to do this without using tags?

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

2 Replies

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

Answer by aldonaletto · Sep 09, 2011 at 02:43 AM

You can use OnMouseDown to detect when the object is clicked, and show the info in a GUIText or GUI.Label, like this:

var information: String; // define the text here private var guiOn = false; private var rect: Rect;

function OnMouseDown(){ guiOn = true; // enable gui and define position at point clicked rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100); yield WaitForSeconds(5); guiOn = false; }

function OnGUI(){ if (guiOn){ GUI.Label(rect, information); } } NOTES:
1- Attach this script to each object.
2- OnMouseDown doesn't work in the iPhone.
3- All objects must have a collider or be a GUI element.

EDITED:
If you already have some common script attached to all blocks, you can just edit this script and add the code above - code changes made to a script affect all instances (except changes in initial values of public variables - these are kept by the Inspector).

Anyway, if you want to have only one script (maybe attached to the camera) you can do a raycast and find the object "under" the mouse pointer:

function Update(){

 if (Input.GetMouseButtonDown(0)){
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     if (Physics.Raycast(ray, hit) && hit.transform.tag=="Block"){
         var clickedObj = hit.transform;
         // do whatever you want with clickedObj
     }
  }

} The problem here is: how to know which information show for each block? To do that, you should have the information stored in each block, or some kind of id number to use as an index and retrieve the information from an array. Both have the same problem: you must attach a script to each block, and write the information or id number for each one in the Inspector.
Finally: you can use the object name or tag to store information - but I don't know which are the limitations (name size, tag size, how much tags you can register etc.).

EDITED 2: Since you need only one territory name showing at one time, you should use a single separated script to show the name (attach it to the camera or any other object), and set variables in it when some territory is clicked. This also helps to set the message Box characteristics in the Inspector with the variable style. That's the separated script - let's call it ShowName.js:

static var information: String; // the territory defines this string... static var rect: Rect; // the message position... static var endTime: float = 0; // and the time it will appear var style: GUIStyle; // you can set color, background etc. at the Inspector

function OnGUI(){ if (Time.time

var information: String; // define the text here, as before

function OnMouseDown(){ ShowName.information = information; // define the name and the position (below) ShowName.rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100); ShowName.endTime = Time.time + 5; // name will appear for 5 seconds } EDITED 3: Now there's the version with a static right aligned box and with variable height:

static var information: String; // the territory defines this string... static var endTime: float = 0; // and the time it will appear var style: GUIStyle; // you can set color, background etc. at the Inspector var w: float = 300; // box width

function OnGUI(){ if (Time.time

var information: String; // define the text here, as before

function OnMouseDown(){ ShowName.information = information; // define the name ShowName.endTime = Time.time + 5; // name will appear for 5 seconds }

Comment
Add comment · Show 6 · 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 sriram90 · Sep 09, 2011 at 03:43 AM 1
Share

aldonaletto , your concept is absolutely correct. but,there is 36 objects in his game if we attach into every object the process will be slow. so, we can tag every objects in a single script. from that we can find object name or object tag in run time. correct?

avatar image aldonaletto · Sep 09, 2011 at 11:05 AM 1
Share

@sriram90: this is possible, but you will have to define the information for each block anyway - I edited my answer to show the approach you've suggested.
On the other hand, if you already have any common script attached to all blocks, you can just edit this script and add the code above - all instances use the same source script, so they will get updated automatically.

avatar image sketchers1 · Sep 10, 2011 at 03:32 AM 1
Share

The First one Worked! $$anonymous$$ind of.... but my only problem is that, I need a box, because the text kind of fades in into the background... Where and how would I put that? and if I want to switch the territory information I am looking at... What changes would I need. Because, I have territory one selected. When I click it, it says bob. When I click the next one, the bob stays, but fred also pops up for the other one. I want the bob to disappear. Does that make sense??

avatar image aldonaletto · Sep 10, 2011 at 04:44 PM 1
Share

In this case, maybe the second script could be more indicated. But you can still use the first one, with some changes: you would need a separated script to show the message (it could be attached to the camera or to a empty object) and use some static variables to communicate which is the currently selected territory. I've edited my answer to show this idea.

avatar image CHPedersen · Sep 12, 2011 at 11:50 AM 1
Share

This is a great, exhaustive answer. If it helped you, be sure to award @aldonaletto an upvote by hitting the thumbs-up button and to mark the answer as correct. :)

  • from me.

Show more comments
avatar image
1

Answer by sketchers1 · Sep 11, 2011 at 04:46 AM

Thanks So much! that helped alot. of course, i still have one more problem. The Box and Information shows up below the mouse... I want it to show up in the same place every time. (right corner of the screen) how would I apply this? Gui Box instead of rectangle? or... how would I do that. Thanks. :)

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 aldonaletto · Sep 12, 2011 at 11:13 AM 0
Share

Just define the rect yourself in GUI.Box (use Screen.width to calculate the left coordinate):

 GUI.Box(Rect(Screen.width-300,10,300,100), information, style);

You can remove all the rect stuff from the other lines in both scripts.

avatar image sketchers1 · Sep 12, 2011 at 08:20 PM 0
Share

Thanks!!! It Works Now! although i still have one problem... my descriptions go out of the box.... is there a way to set a limit so that the automatically skip to the next line? Once again, Thanks!!!

avatar image aldonaletto · Sep 14, 2011 at 05:11 PM 0
Share

You must set Word Wrap in the style variable at the Inspector - this will break the lines. Case the text is too big, it will overflow down the box. You can use a GUI function to calculate the height based on a given width and text:

    var h = style.CalcHeight(GUIContent(information), 300);
    GUI.Box(Rect(Screen.width-300,10,300,h), information, style);
I edited the answer to show these last modifications - including a w variable to set the box width.
avatar image sketchers1 · Oct 07, 2011 at 06:29 PM 0
Share

oh... i have come upon another problem. I have a lot of information typed in word.. I wanted to copy it and paste it into the Information option but it wont let me... is there a way to fix that?

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

Detect a click outside a GUI/object 4 Answers

Gameobject shown as GUI 1 Answer

Many objects - optimization 1 Answer

Get texture from gui to gameobject 1 Answer

how can i detected wheter is clicked a children of gameobject? 0 Answers


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