Scripting OnClick()

Alright, my logic is all wrapped up in it self and I am hoping for some help.

I am generating a grid, 4X4, and trying to assigning each button to its own OnClick().

My apparent problem is that when it assigns the OnClick() to my CardClicked(k,l);, it sends it in the variable state. Meaning that when I build my deck with k and l, all of the OnClicks() are assigned to the very last card because the value of k and l will always be those numbers.

Can I loop assign OnClick()s in a way where they stay what they are when being assigned using variables?.

I am also willing to accept the fact thatI may be going about this all wrong, I am create a memory card game using the GUI, it is the example in the book i am learning from, however it was published prior to 4.6 and I am trying to accomplish the same task but with uGUI and it is a bit more of a challenge it seems. Not that I don’t like a challenge, it has helped me learn a great deal about Unity. Just keep hitting snags.

for(var k:int =0; k<rows;k++)
		{
			cardBack=backPanel.transform.GetChild(k);
			cardFace=facePanel.transform.GetChild(k); //first child of canvas
			//First row of Canvas
			for(var l:int=0; l<cols; l++)
			{
				cardButtonBack=cardBack.transform.GetChild(l);
				cardButtonFace=cardFace.transform.GetChild(l);
				cardButtonFace.GetComponent(UnityEngine.UI.Image).sprite=aGrid[k,l].pic;
	        	//Rename button so that it has grid coordinates in it and can be referenced by treating the
	        	//String as an array and reading [6] for x and [7] for y!!
	       		cardButtonFace.name=cardButtonFace.name + k + l;
	       		cardButtonBack.name=cardButtonBack.name + k + l;
	       		buttonBuilder=cardButtonBack.GetComponent(Button);
	       		buttonBuilder.onClick.AddListener(function()
				{
					CardClicked(k,l);
					Debug.Log(cardButtonBack.GetComponent(Button));
	       		
				});

	        }
	    }

How about have the OnClick() calls a routine in a script attached to that button.

The script has a public panel and that’s where your card appears as a child.

So when OnClick calls the function, lets call it BeenClicked() it looks at that panel and gets the child object and reads the name from that child.

Does that help? I can visualize the script already so let me know if you need a hand with that.

You only need one script but it needs to be on each button and the public panel will be different for each.

EDIT:

Sure you can reference a script from another script, you need to be able to find the GameObject that script is on the GetComponent for the script. You can then access that script and its public components.

I don’t know what your GameObjects or scripts are called but say you hold the main script on the Canvas and rename to CardTableCanvas and your script is called MainScript then:

private MainScript theMainScript;
public RectTransform MyPanel;

void Start()
{
    theMainScript = GameObject.Find("CardTableCanvas").GetComponent<MainScript>();
}

public BeenClicked()
{
    theMainScript.publicBool = true;
}

So from the all buttons with that script you can now change the Bool to true, you can even create a public function in the MainScript that can be sent the parent RectTransform or just the card name.

EDIT 2:

Should add that if you hold the can player click on the actual button script then it can just reference the child of the RectTransform so it doesn’t get clicked.

I’ve been working on the assumption that there’s a button below the card but it’s sounding more like you just treat the card as the button.

I don’t think that should cause any problems but let me know if you need anything else to get this working.