Search Unity

Inccorect objects positions using Instantiate().

Discussion in 'Scripting' started by notayyet, Nov 10, 2020.

  1. notayyet

    notayyet

    Joined:
    Nov 8, 2020
    Posts:
    3
    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).
    Code (CSharp):
    1.  
    2. public class ClickAnimationScript : MonoBehaviour
    3. {
    4.     public GameObject AnimationPrefab;
    5.     private Vector2 ScreenBound;
    6.     void Start()
    7.     {
    8.         ScreenBound = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    9.     }
    10.     public void SpawnAnimation()
    11.     {
    12.         GameObject NewText = Instantiate(AnimationPrefab) as GameObject;
    13.         NewText.transform.position = new Vector2(ScreenBound.x, ScreenBound.y);
    14.  
    15.     }
    P.s this red pixel is my camera.
    Screenshot_2442.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    This looks fishy:
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1.         GameObject NewText = Instantiate(AnimationPrefab) as GameObject;
    You don't need
    as GameObject
    here.
     
    Joe-Censored likes this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @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.
     
    Last edited: Nov 10, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    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:

    Code (csharp):
    1. public GameObject TheChunkOfUIPrefab;
    2.  
    3. public RectTransform WhereIWantThatChunkSpawned;
    4.  
    5. var TheInstantiatedChunk = Instantiate<GameObject>(
    6.                         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.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    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.
     
    Last edited: Nov 10, 2020
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    PraetorBlue likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    You made me look! :) 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.
     
  8. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    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.
     
    PraetorBlue likes this.