Search Unity

Event doesn't work when Instantiate RectTransform element.

Discussion in 'UGUI & TextMesh Pro' started by Rodyfish, Sep 4, 2014.

  1. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Hey Guys.

    I'm trying to create a dynamic menu system, where the level buttons will be generated from a prefab via some attached code (CSharp).

    The problem is, I can't get the events to work.

    It works fine if I drag the prefab unto the Canvas before running the project, but not if I generate it on the fly.

    To be more specific - I try to make a PointerClick Event. Image on left is the two initial elements, and right is after code project is started.

    Do you have any idea why?

    upload_2014-9-4_15-56-23.png

    Code (CSharp):
    1.  
    2. public RectTransform panel;
    3.     public RectTransform levelButton;
    4.  
    5.     public RectTransform _canvas;
    6.     public EventSystem _eventSystem;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         for(int i = 0; i < 46; i++)
    12.         {
    13.             RectTransform t = Instantiate(levelButton) as RectTransform;
    14.  
    15.             int levelScreenIndex = Mathf.FloorToInt(i/10.0f);
    16.             int relativeI = i - levelScreenIndex*10;
    17.  
    18.             t.position = new Vector3(0.166667f*(relativeI%5 + 1)*panel.rect.width + _canvas.rect.width*levelScreenIndex,
    19.                                     1-0.3333333f*(Mathf.FloorToInt(relativeI/5.0f) + 1)*panel.rect.height,
    20.                                     0);
    21.             Text text = t.FindChild("Text").GetComponent<Text>();
    22.             if((i+1) < 10)
    23.                 text.text = "0"+(i+1).ToString();
    24.             else
    25.                 text.text = (i+1).ToString();
    26.  
    27.             t.transform.transform.SetParent (panel, false);
    28.         }
    29.     }
    30.         }
     
    Last edited: Sep 4, 2014
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    I'd have to see the scene to know why this might be happening. Is there something spawned that is blocking the clicks in the runtime version maybe?
     
  3. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Is there anyway I can show you the project (sorry, I'm new at this community thing).

    I have tried to use the
    Code (CSharp):
    1. _eventSystem.currentSelectedObject.gameObject.name
    command, but nothings shows in the command line, so I don't think is has anything to do with the UI system. Can the problem be within the order of commands in the code shown before?
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    If you can make a small repro scene that exhibits the problem then attach it to a post I can take a look.
     
  5. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Here ya go! It should work. And thanks for looking into it! :)
     

    Attached Files:

  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Ah! So 2 things:
    1)
    On your prefab Button you have a reference to the CubeController, the problem is this isn't referring to the Cube Controller in the scene, it's refering to the one in the prefab. Because it's a project asset and not an in scene asset there isn't anything that can really happen here.

    What you need to do is remove the callback from the button prefab, then when you create the instance you should hook up the scene cube. Something like:

    Code (csharp):
    1.  
    2. RectTransform t = Instantiate(levelButton) as RectTransform;
    3.  
    4.             var button = t.GetComponent<Button> ();
    5.             button.onClick.AddListener (
    6.                 delegate
    7.                 {
    8.                     Debug.Log ("asdf");
    9.                     _controller.RotateCube(1440);
    10.                 } );
    11.  
    12.  
    2) Your panel is another canvas. This is fine, but there is no GraphicRaycaster on the canvas. You need to add one to have raycasting working. You have actually done a performance optimization because right now no raycasting is happening against those graphics ;)
     
  7. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Oki thanks for the really great answer. The cube thing was just something I tried, since it wouldn't work.

    But I didn't knew that the panel is a canvas. The other UI elements don't have a GraphicRaycaster as well (and theywork!).
     
  8. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Oh I just realized that for some reason, I've attached a Canvas component. How silly :).
     
  9. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Yeah that was it :)