-1

I know this maybe a duplicate of How to detect click/touch events on UI and GameObjects but I tried actually what's in there . But my problem still exists.

Here's my code

GameObject o = null;
private void Start()
{
    for (int i = 0; i < 6; i++)
    {
        o = Instantiate(obj) as GameObject;
        o.transform.SetParent(pos_obj);
        o.transform.localScale = Vector3.one;
        o.transform.name = "chips " + i;
       
        o.transform.localPosition = new Vector3(0, 0, 0);
        NGUITools.SetActive(o, true);

        UIGridReposition(UIGrid.Sorting.Vertical, true);
    }
}

This line of code above is how I instantiate my sprites and it is like this on my heirarchy

chips 1

chips 2

chips 3

chips 4

chips 5

Now when i try to put this line of code in the UI Button

public void TestClickEvent(){
   Debug.Log("This object is :" + o.transform.gameobject.name);
}

Now when I click on the Instantiated object only chips 5 will only be the output on my console. Even if i click the first,second etc Instantiated Object

Can someone please help me .

What I am trying to do is to get the designated number of each Intantiated Object for example

If i click chips 1 then it will output This object is : 1;

Community
  • 1
  • 1
Ketskie
  • 15
  • 7
  • I put the `Button button = o.GetComponent – Ketskie Jul 26 '18 at 06:51
  • @Programmer By the way sir it is a `sprite` that has a `ui button` – Ketskie Jul 26 '18 at 06:56
  • yes im using NGUI so your code is not possible sir? – Ketskie Jul 26 '18 at 06:57
  • That's odd. I'm not entirely sure about NGUI. But somewhere in the code is a call to `TestClickEvent()', which is being assigned to all buttons, pointing to the last instantiated object. If that's all your code, then it would seem it's in `NGUITools.SetActive(o, true);`, but that makes no sense. – Milan Egon Votrubec Jul 26 '18 at 06:58
  • You have to post a screenshot of the GameObject with the components attached to it. Forget it if you're using NGUI since I haven't worked with that for a long time and no longer use it. This will oonly work with Unity's UI – Programmer Jul 26 '18 at 07:00
  • So its still useless i guess . Because I am using NGUI – Ketskie Jul 26 '18 at 07:03
  • @Programmer I saw this post and you helped him actually . [https://stackoverflow.com/questions/44579727/raycast-wont-hit-collider-after-using-ngui](https://stackoverflow.com/questions/44579727/raycast-wont-hit-collider-after-using-ngui) . But can figure out the rest of the code he used – Ketskie Jul 26 '18 at 07:16
  • I know you found a solution but I think you should be using the built-in event system instead of raycast when detecting clicks on UI objects.. I dug through my old code and found something. If that doesn't work then use your raycast method – Programmer Jul 26 '18 at 07:33

2 Answers2

1

You're using NGUI and the way to detect click event is totally different than the way you would with Unity's UI. When detecting a click, a raycast may work but is not the recommend way to do so. Always use the callback events for that.

You can do this with UIEventListener.

GameObject o = null;
private void Start()
{
    for (int i = 0; i < 6; i++)
    {
        o = Instantiate(obj) as GameObject;
        o.transform.SetParent(pos_obj);
        o.transform.localScale = Vector3.one;
        o.transform.name = "chips " + i;

        o.transform.localPosition = new Vector3(0, 0, 0);
        NGUITools.SetActive(o, true);

        UIEventListener.Get(o).onClick += TestClickEvent;

        UIGridReposition(UIGrid.Sorting.Vertical, true);
    }
}

void TestClickEvent(GameObject sender) 
{ 
    Debug.Log("Clicked: " + sender.name); 
}

There is really no clear examples for NGUI out there so expect to go through a lot of stuff to complete a simple task.

Programmer
  • 121,791
  • 22
  • 236
  • 328
0

Found my solution instead of Camera.main I tried UICamera.currentCamera instead

public void TestClickEvent()
{
    Vector2 point = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
    Ray ray = UICamera.currentCamera.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, 100))
    {
        Debug.Log("I hit something :" + hit.collider.gameObject.name);
    }        
}
Ketskie
  • 15
  • 7