Inccorect objects positions using Instantiate().

When I use the Instantiate to copy an object (text with animation) using prefabs, the objects are not in the correct position in space. My object is in the canvas, which is tied to the camera, however, the copied object is created in a completely different place (the coordinates seem to be correct, but if you start changing them, then there will be thousandth coordinates). I tried to manually set the coordinates, only when using the camera coordinates the object is at the same z-coordinate as the canvas, but it still has huge dimensions. How to fix it? I want to clarify that I am using a 3D project template(but so far I do not have 3D objects, and this object also 2D).

public class ClickAnimationScript : MonoBehaviour
{
    public GameObject AnimationPrefab;
    private Vector2 ScreenBound;
    void Start()
    {
        ScreenBound = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }
    public void SpawnAnimation()
    {
        GameObject NewText = Instantiate(AnimationPrefab) as GameObject;
        NewText.transform.position = new Vector2(ScreenBound.x, ScreenBound.y);

    }

P.s this red pixel is my camera.

This looks fishy:

Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));

ScreenToWorldPoint is expecting screen-space coordinates, but you’re passing the world-space z coordinate of the camera into it. Why? I think the third parameter there should most likely just be 0.

Also: GameObject NewText = Instantiate(AnimationPrefab) as GameObject; You don’t need as GameObject here.

1 Like

@notayyet

“My object is in the canvas”

What exactly are you trying to do?

So do you have a uGUI Canvas, and are you trying to place a Prefab that is a RectTransform to that Canvas?

Or is it something else you are doing?

“I tried to manually set the coordinates, only when using the camera coordinates the object is at the same z-coordinate as the canvas, but it still has huge dimensions. How to fix it?”

Canvas doesn’t work with camera coordinates. Only Screen Space Overlay canvas can in some cases look like it works in world units.

Actually this Z is how far ahead of the camera in the world you want it to be… if you give Z == 0 then all your results will be the same: the camera position, regardless of what other arguments you put in.

As for @notayyet , if something you are instantiating uses a Canvas and/or contains Canvas renders, it must be parented appropriately.

The simplest way to do this without error is to use one Instantiate instruction:

public GameObject TheChunkOfUIPrefab;

public RectTransform WhereIWantThatChunkSpawned;

var TheInstantiatedChunk = Instantiate<GameObject>(
                        TheChunkOfUIPrefabb, WhereIWantThatChunkSpawned);

If you fail to parent it, you will never see it.

If you parent it with a plain old .SetParent() call without the correct second argument, or by assigning to the .parent property, you also will never see it.

Keep it simple, instantiate UI things the way listed above. It works 100% of the time.

I’ve read this in another thread and I still don’t believe it’s true. I’m going to go test it out now, but I believe if you give it z == 0 you get a point on the camera’s near clipping plane.

Edit: I tested it, I’m wrong.

1 Like

The documentation:

explicitly promises this:

The z position is in world units from the camera.

1 Like

You made me look! :slight_smile: I mean the near plane WOULD have been a reasonable choice, but it is not the choice they made, probably because of matrices and discriminators and distances and lord knows what other linear math.

I feel like this is the Mandala Effect… I have distinct memories of using ScreentoWorldPoint with a z of 0 and getting a result that wiggled a bit.

1 Like