• 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 The_Toller · Sep 30, 2015 at 06:26 PM · c#uiunity 5.2

Need to set a canvas on screen at a position relative to a game object - no longer working!

Okay, so this have been going on all day. In my game, when I select an object, I want the healthbar and name etc to be displayed on my UI screen. I don't want it in worldspace due to the scaling etc. This was one of the first things I implemented in my game so it's been working flawlessly since day one. However, today when I was thinking I should work on a complete different part of the game, this no longer works. The only thing I know for certain that has changed today is that I updated from 5.0 to 5.2. All tough I don't see any reason for this to break because of this - it's the only known change. The issue is that I can't seem to change the position of the HUD element. It no longer follows my game object but is stuck at the center of my screen. The position is updated via a coroutine.

Error one: When I first started out today, the HUD was updating it's position true enough, but it did not at all follow my object anymore. If my object was in the center of my screen, so was my HUD. If i panned my object to the far left, the HUD had moved about 10% of the screen to the left and so on. So it was moving, but not at all as expected.

Thought solution: During debug, I commented away parts of my follow code as well as threw some debug.logs in the mix to pinpoint the issue. The information I extracted from my debug.log was that the objects position, when converted to viewport points - seemed correct and the weird thing is that the debug.log of the rectTransform's 'anchoredPosition' were indeed that same value all the time. I thought I'd start over so I commented it all out and oddly enough, it still moved! Still by that small small small amount! So I thought, hey - is this because of the element being a child of the object? (I set it as a child to keep my hierarchy clean, mostly) and when I removed it as a child the movement stopped indeed. Which hit me right in the stomach with:

Error two: I no longer set the element as a child of my object. I don't see why this would be an issue since it wasn't in the first place but now I've been going at it for twelve hours and I'm trying to "just start over". But it's still at the center of my screen, no mather what. Now it does not move even those 10%. Debug.Log tells me that the anchoredPosition keeps updating to my viewport point when I move the object around and all is well. But the canvas displaying my HUD is static.

Tried solutions: I have tried everything. Except of course, the working solution. I have played around with the settings for the canvas (Screen space - overlay, screen space - canvas) etc. I have tried using WorldToScreenPoint instead of WorldToViewportPoint - same result... Here's my code:

 private IEnumerator SetPosition()
     {
         RectTransform canvasRect = gameObject.GetComponent<Canvas>().GetComponent<RectTransform>();
         while(true)
         {
             Vector2 viewportPosition = Cameras.hud.WorldToViewportPoint(hudOf.transform.position);
             Vector2 hudScreenPosition = new Vector2
                 ((viewportPosition.x * canvasRect.sizeDelta.x),
                 (viewportPosition.y * canvasRect.sizeDelta.y));
             canvasRect.anchoredPosition = hudScreenPosition;
             yield return null;                                    
         }
     }

Comment
Add comment · Show 2
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 D3Duck · Sep 30, 2015 at 07:07 PM 0
Share

It's not really an answer to your question, but what is the problem with scaling? you always want it to be the same size on the screen? Otherwise, making it in world space would be much easier.

Im not fully clear on your hierarchy. So you have an object without child in the game world, and a Canvas component as child of the canvas that should follow it? This would help me understand your problem.

avatar image The_Toller D3Duck · Sep 30, 2015 at 08:17 PM 0
Share

I guess that would be a solution for it. And if I can't get this to work, then I'd look into a method for scaling it in the world. But if I do that, and it works - I'll still always know that I had another way of it working flawlessly without scaling and I'll never know why it suddenly broke or why I can't get it to work again. :P

I have my game objects, and when selecting them i either instantiate a new HUD prefab (canvas with labels and sliders and such) or grab an inactive instance of it and set it as a child of my game object. This is not really necessary and it's just for not cluttering up my hierarchy with a lot of HUDs. :)

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Почему после перехода на другую версию Unity при создании кнопки она не откликается на геймпад и клавиатуру? 0 Answers

Overlay 2 scenes 1 Answer

How to scale UI PointerEventData.delta propertly 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