Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to instantiate a canvas to a custom world space position?

Discussion in 'Scripting' started by Deleted User, Feb 24, 2018.

  1. Deleted User

    Deleted User

    Guest

    I usually ask my questions on Unity Answers, but for some reason it's not letting me this time.
    -------------------------
    I'm trying to instantiate a canvas in world space to the same position of an object, yet every time, no matter what I try, it always instantiates to (0, 0, 0). Even after instantiating, I can't seem to change the position of it, even though there is absolutely no other code interfering with the position of that canvas.

    var clone = (GameObject) Instantiate (canvasPrefab, transform.position, Quaternion.Euler(Vector3.zero));

    This is the line of code I use, which used to work in an older project of mine, but now that I updated to the latest version of Unity, it also doesn't work in that project anymore.

    Does anyone know what the new and proper way of instantiating a canvas in a custom position is?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Hmm, works fine for me. Are you sure transform.position is not (0,0,0)? Easiest way to check is to just do a Debug.Log( transform.position.ToString()); right before you instantiate.

    Perhaps your script is running on a GameManager-like object that is at (0,0,0) and you are expecting it to be on the mobile object where you want the canvas to be?
     
  3. Deleted User

    Deleted User

    Guest

    var clone = (GameObject)Instantiate (notePrefab, new Vector3 (transform.position.x, transform.position.y + 1, 0), Quaternion.identity);
    Using (almost) the same line of code, but from a different project, it still doesn't work.
    I tested some other projects that had this line or a variation of it (and got updated to Unity 2017) and all of them don't work anymore. But it does still work in a project that hasn't been updated to Unity 2017.
    It doesn't matter if I want it to copy the position of a transform or if set the custom position myself, it always instantiates at 0,0,0.