Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Redwolve · Jan 12, 2015 at 05:37 PM · uionclicklistener

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));
                    
                 });
 
             }
         }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · Jan 12, 2015 at 06:05 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Redwolve · Jan 12, 2015 at 06:50 PM 0
Share

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); } } }

avatar image Redwolve · Jan 12, 2015 at 07:41 PM 0
Share

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?

avatar image Mmmpies · Jan 12, 2015 at 07:48 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges