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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do I instantiate an object at runtime and place it in new GUI screen space?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Spankenstein, Oct 26, 2014.

  1. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25
    My GUI canvas is in Screen Space - Overlay mode.

    When I instantiate an object and try and place it on an object inside the GUI, the coordinates are always incorrect as they are not in the correct coordinate space.

    For example:

    Code (csharp):
    1.  
    2.             var rt = GetComponent<RectTransform>();
    3.  
    4.             var o = Instantiate(myObject) as GameObject;
    5.             o.transform.position = rt.transform.position;
    6.             o.transform.parent = rt.transform;
    7.  
    The object is in the correct position when viewed in the Scene view but it is not correct when viewed in Game view.

    How can I get the object to be in the correct coordinate space?
     
  2. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25
    Figured it out. I required a Canvas Renderer component on the GameObject.

    Why does Unity complain when trying to make a Prefab out of a newGUI object?