Search Unity

Finding a specific canvas? Is this normal?

Discussion in 'Scripting' started by Napivo, Feb 27, 2015.

  1. Napivo

    Napivo

    Joined:
    Feb 9, 2015
    Posts:
    39
    I am looking for mu debug UI canvas and I came up with this code. I feel it is quite excessive.

    Code (CSharp):
    1. //Find the UI Canvas
    2.         Canvas = GameObject.Find ("CanDebugUI").GetComponent<Canvas>();
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    As long as you don't do that frequently (e.g. in Update) it's alright.
    You may also make a public instance variable in your class and drag&drop the object onto the slot in the inspector.
     
    Napivo likes this.
  3. Napivo

    Napivo

    Joined:
    Feb 9, 2015
    Posts:
    39
    This is for a standalone class, I use for debugging purposes. It is not linked to the inspector.

    I only search for the objects during the constructor and store the references for later use, so that should be fine.

    I wish there was an easy way to generate objects like canvas, text, panels and buttons for the new UI but is very design driven.

    Thanks for the reply.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can Instantiate prefabs, like any other GameObject.

    If you are hard core you can simply create a new GameObject and use AddComponent to build it up.

    The only option that is really missing out of the box for on the fly generation of UI is nested prefabs. They promise us its coming.