- Home /
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;
}
"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)?
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.
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.
Your answer
Follow this Question
Related Questions
C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer
how to destroy camera instatiated from prefab? 0 Answers
How do I stop Script from making multiple Cameras? 1 Answer
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Instantiating prefab camera 2 Answers