• 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 juniperspark · May 19, 2016 at 07:20 PM · camerainstantiateworldtoscreenpoint

WorldToScreenPoint not working with Instantiated Object.

I want to have an object instantiate at a certain position and then have a floating text and a button overlaid at the objects position. I cannot have these objects as world UI as they warp with the camera perspective when I animate them and want the text the same size even if some objects are further away.

So the code I have works if I have an object already in my scene and click start. If I try to instantiate the object, the code does not work. Any ideas?

Script #1

 var object = Instantiate(Resources.Load("Object", GameObject), new Vector3(5, 0.5, 0), Quaternion.identity);    

Script #2 (placed on the prefab object)

 var gOTransform        : Transform;
 var button            : GameObject;
 var text            : GameObject;
 var cameraMain         : Camera;
 
 function Start()
 {
     var cameraPos : Camera = cameraMain.GetComponent(Camera);
     var screenPos : Vector3 = cameraPos.WorldToScreenPoint(gOTransform.position);        
     button.transform.position = screenPos;    
     text.transform.position = screenPos;
 }


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 tanoshimi · May 20, 2016 at 08:06 PM 0
Share

"does not work" in what way? What errors does it produce? Is that your whole script? Where do you assign values to gOTransform, button, text, and camera$$anonymous$$ain? Why do you call camera$$anonymous$$ain.GetComponent(Camera) when camera$$anonymous$$ain is itself already a camera component? (And why do you call the result "cameraPos", when it's not a position)?

avatar image juniperspark tanoshimi · May 20, 2016 at 08:54 PM 0
Share

Hi,

The script #2 is placed on a prefab and the variable values are assigned in the inspector. So I have drag and dropped each 'object' (GO = current object attached to - could be 'this', button = child of GO, text = child of GO, camera = already in scene) into the slots in the inspector and then saved this as a prefab, so those values will be ready on instantiate.

This was all of my code for the task.

CameraPos was named as such as I copy and pasted the example from the unity docs. Yes, cameraPos variable isn't neccessary and could just be camera$$anonymous$$ain.

There are no errors that come up. The only problem is that the UI does not update to the correct position - it stays at the default and does not move.

It does work if I already have a gameobject in the scene with my scripts attached. but as soon as I want to instantiate the object it doesn't work. I think it must be something to do with the order the code is updated in maybe.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 20, 2016 at 08:04 PM

If the Button and Text are using RectTransforms, then you need to use the RectTransform's anchoredPosition property, not Transform's position property. But, that's assuming you're using a canvas with UGUI. Since they are GameObject references, I can't tell.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

56 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 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

C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer

How to reposition camera so that given plane point is in given screen position? 1 Answer

How do you change the background of an instantiated camera? 0 Answers

Proper use of Camera WorldToScreenPoint 1 Answer

Need help putting an object at a point based on screen coordinates 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