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. Dismiss Notice

Scripting the new UI

Discussion in 'UGUI & TextMesh Pro' started by FirebladeBR, Sep 12, 2014.

  1. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Hi all, I tried to post the doubt on the developer preview area but i found out that my problem is with scripting...

    So I hope you guys can help me just write this simple script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. function Start () {
    6.  
    7.     var canvas = new GameObject ("canvas1", Canvas);
    8.     canvas.RectTransform.width = screen.width;
    9.     canvas.RectTransform.height = screen.height;
    10.  
    11.     var button = new GameObject("button", CanvasRenderer, Image, Button);
    12.       button.transform.SetParent(canvas1.transform, false);
    13.  
    14. }
    In the code above I am trying to create a canvas on the screen with the maximum dimensions and to just put a button on the screen.

    I tested the code and nothing happened. No errors and no button created. I would also help if you guys can put me to some examples (I read some manuals on the UI but they just say: do this, but don't show how to proper use a syntax.... :( )

    Thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    There are definitely errors...that code won't compile; look at the console in Unity to see the errors. "canvas.RectTransform" is a syntax error, but you don't need to set the width and height anyway, so just remove those lines. You also didn't define "canvas1" anywhere, so change that to "canvas". When you need to access a RectTransform component, use GetComponent, and look it up in the scripting docs so you can see what the properties are (there aren't any width or height properties).

    --Eric
     
  3. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Eric,

    Thanks for the help. But I tried the code bellow with the modifications that you sugested and nothing happened and also no errors on the console:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. function Start () {
    6.  
    7.      // var canvas = new GameObject ("canvas", Canvas);
    8.    
    9.     var canvas = new GameObject ("canvas", Canvas);
    10.    
    11.     var button = new GameObject("button", CanvasRenderer, Image, Button);
    12.     button.transform.SetParent(canvas.transform, false);
    13.  
    14.  
    15. }
    16.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It definitely works. Try a new project, and make sure you attach the script to an object.

    --Eric
     
  5. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Eric, I tried something different. As you suggested before I will be using prefabs... So here's what I did:

    - I created a new project
    - I putted a canvas on the scene
    - I created an empty prefab and added a button UI to the prefab - called this prefab as "botao"
    - I created a script wich I attached to the canvas
    - I added the botao prefab to the prefab variable on the script
    - I run the program and nothing happens nor any error message:

    Content of the script attached to the canvas:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var thePrefab : GameObject;
    4.    
    5. function Start () {
    6.    
    7.      var instance : GameObject = Instantiate(thePrefab);
    8.  
    9. }
    Am I forgeting something? Cause when i manually add the prefab it shows on the game screen, but when I try to instantiate it it does not do anything.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You didn't parent the prefab instance to the canvas.

    --Eric
     
  7. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Thanks,

    Now it seems to work by I got a really odd behavior, it created 65k+ clones of the button (An error was thrown: Failed to generate mesh for Canvas as more than vertices 65535 are present).

    I just added this to the code above:

    Code (JavaScript):
    1. var canvas = GameObject.Find("Canvas"); //// Canvas is the name of the canvas on the screen...
    2. instance.transform.parent =  canvas.transform;
    3.  
    The start() shouldn't just be called once? Or in the new UI it is called more times?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Is this script on one of the objects you're instantiating? If so, it would call Start(), which spawns a new one, which calls Start(), which spawns a new one, which... on and on ad infinitum, or rather, ad 65535um.
     
  9. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    True, my bad. Don't know why I putted it there.... Thanks