Search Unity

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?