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 octaviomejiadiaz · Nov 13, 2012 at 01:03 AM · guigameobjectbuttonfloatfollow

GUI Button follow GameObject, like floating button how to on C++?

Please help, i have being trying to make that a GUI button follow a GameObject, but ive fail in all my tries..

and the other option is to make that GameObject a touchable element.

somebody told me that i can do it by RayCast but i have tried but fail again. cause i dont know where to put some codes..

please help.

Comment
Add comment · Show 15
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 IgnoranceIsBliss · Nov 13, 2012 at 01:21 AM 0
Share

It's pedantic, but the language is called "C#" rather than "C++".

avatar image WestonD · Feb 26, 2013 at 10:56 PM 0
Share

Why did you post a 2nd question on the same topic? The question you had down thread had the code you were working with and the possible solution that I posted. Though I thought you said you had it working for one object in that thread.

avatar image octaviomejiadiaz · Feb 26, 2013 at 11:14 PM 0
Share

yes, i know, and it works fine for just one GameObject, my question is how to put 4 GUIButtons to follow 4 GameObject, one for each gameobject.

cause Vector3 V = Camera.main.WorldToScreenPoint(cube1.transform.position); i put cube, but that just make work one GUIButton i want 4 GUIButtons.

avatar image WestonD · Feb 26, 2013 at 11:29 PM 0
Share

Apparently the mods haven't allowed my answer to post on the other thread so Ill post it here as a comment. $$anonymous$$y suggestion was to use an array.

     public GameObject cube1;
     public GameObject cube2;
     public GameObject cube3;
     public GameObject cube4;
     public GameObject cube5;
 
     var objectArray = [cube1, cube2, cube3, cube4, cube5];
     var nameArray = ["cube1","cube2","cube3","cube4","cube5"];
     void OnGUI()
 {
     for(i=0, i<objectArray.length,i++){
     Vector3 V = Camera.main.WorldToScreenPoint(objectArray[i].transform.position);
        if (GUI.Button(new Rect(V.x,Screen.height - V.y,300,200),nameArray[i])) {
           Do Something Here;
           }
     
     }
 }


The above is java script, had to look up how you do arrays in C#. I think you would just replace the array defines thusly:

 GameObjects[] objectArray = new GameObjects[5] {cube1,cube2,cube3,cube4,cube5}
 
 string[] nameArray = new string[5[ {"cube1","cube2",...etc]
 
 and it would be i<objectArray.Length  the L being capitalized.
 
 
 
 
avatar image octaviomejiadiaz · Feb 26, 2013 at 11:40 PM 0
Share

I've just put var objectArray = [cube1, cube2, cube3, cube4, cube5]; var nameArray = ["cube1","cube2","cube3","cube4","cube5"];

and says that its wrong.. alt text

screen shot 2013-02-26 at 5.38.21 pm.png (64.0 kB)
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IgnoranceIsBliss · Nov 13, 2012 at 01:21 AM

If you want a 3D object to follow them, use a GameObject that is a child. Yes, you'll use a Raycast to detect a click or touch - there are quite a few answers on these forums that will help you figure out how to do it.

Rather than saying 'Tried and Failed', send us what you've done so we can tell you what may be wrong. You can put the the RayCast code anywhere you want, really - but it's normally put in the 'Update' function of a Component that is attached to the camera.

If you want a GUI.Button to 'follow' a 3D object, you'll need to translate the world-position of the object to a screen-position.

You do that with Camera.WorldToScreenPoint - so for example, you'd have code like this...

 void OnGUI()
 {
     Vector3 V = Camera.main.WorldToScreenPoint(FollowingObject.transform.position);
 
     if (GUI.Button(new Rect(V.x,Screen.height - V.y,100,30),"Button Text");
 }

The first line converts the position of your 3D object into a 2D point location on your screen. The second line draws the button.

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 octaviomejiadiaz · Feb 26, 2013 at 10:50 PM

sorry to ask again, i've lost all my work cause my computer had a problem.. but i'm coding again this thing. and all works fine. but i can't make that different GUIButtons Follow different cubes(GameObjects).

i have cube1,cube2,cube3,cube4, where do i can put the name of this GameObjects? or what do i have to do?

     void OnGUI()
     {
         Vector3 V = Camera.main.WorldToScreenPoint(cube1.transform.position);        
             if (GUI.Button(new Rect(V.x,Screen.height - V.y,300,200),"Button Text"))
     }
     

 
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

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

11 People are following this question.

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

Related Questions

Detect Click on Gameobject 0 Answers

GUI Button Position - Can it float to the top-left? 3 Answers

How do I make individual buttons change individual variables? 0 Answers

Cube click pop-up menu 0 Answers

GameObject touchable instead GUI button 3 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