- Home /
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));
});
}
}
you mean CardClicked(k,l);
will always be the same number because k,l continuously change? I am just guessing but to me your problem appears to be that buttonBuilder is a single variable the the entire for loop changes all the time. I could be wrong but at first glance, it looks as if you need to have int idNumber = int.Parse(k.ToString() + bl.ToString()); buttonBuilder[idNumber] = cardButtonBack.Getcompnent(Button); buttonBuilder[idNumber].onClick.AddListerner(function(){ CardClicked(k,l);....
But I could be wrong, that is why I put this up as a comment.
Answer by Mmmpies · Jan 12, 2015 at 06:22 PM
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.
That works for making it so i can flip the cards over, problem is i also want to enable the players ability to click, which would require variables from the main script and even with them being public i cant use them in a function that is on a seperate script. Is there a way to do that? that would help.
my new script i have written is as follows:
function Clicked(clicked:GameObject)
{
if(clicked.GetComponent(Button).animator.GetBool("isFaceUp"))
{
clicked.GetComponent(Button).animator.SetBool("isFaceUp",false);
}
else
{
clicked.GetComponent(Button).animator.SetBool("isFaceUp",true);
}
}
However I would like to reference the public var canPlayerClick.
function Clicked(clicked:GameObject) {
if(canPlayerClick){
if(clicked.GetComponent(Button).animator.GetBool("isFaceUp")) { clicked.GetComponent(Button).animator.SetBool("isFaceUp",false); } else { clicked.GetComponent(Button).animator.SetBool("isFaceUp",true); } } }
Once again thank you very much. Took a little to get the JS version of that correct, but I did and it worked.
Does this work both ways? Can I change the mainscripts variables through this method as well?
Yes as long as they're public you should be O$$anonymous$$ but be careful.
I guess you'll use those scripts time after time, if you have them on card as it Instantiates (if that's what you're doing) then it should start with default values if you just have it attached to a button under the card then make sure you clear out the old values back to default when you deal extra cards.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
button.onClick.AddListener(method); NOT Working 1 Answer
AddListener() Javascript 2 Answers
onClick listener not being added properly via script, but no errors 0 Answers
onClick.AddListener doesn't get assigned to button. 1 Answer