Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CHIRANJIT · Apr 13, 2017 at 12:48 PM · unity 5unity 2d

Unable to click/touch event on sprite

Hi, i am new in unity development and i am developing a card game on unity. My question is how to add click event on each card. I have added 52 cards dynamically from Prefabs, index like 0...51. How to determine which card is clicked. OnMouseDown() is working but how to determine for which card its fired.

void AddCard(Vector3 position, int cardIndex, int positionalIndex) {

     if (fetchedCards.ContainsKey (cardIndex)) 
     {
         if (!faceUp) 
         {
             CardModel model = fetchedCards [cardIndex].Card.GetComponent<CardModel> ();
             model.ToggleFace (fetchedCards [cardIndex].IsFaceUp);
         }

         return;
     }
         
     GameObject cardCopy = (GameObject)Instantiate (cardPrefab);

     //Debug.Log ("Prefab Name " + cardCopy.GetComponent<Button>().name);
     //cardCopy.transform.SetParent(ParentPanel, false);
     //cardCopy.transform.localScale = new Vector3(1, 1, 1);

     //Button tempButton = cardCopy.GetComponent<Button>();
     //UnityEngine.Events.UnityAction action1 = () => { this.btnClicked("testarg"); };
     //tempButton.onClick.AddListener(action1);
     //tempButton.onClick.AddListener((delegate { OnClickedButton(cardIndex);}));
     //tempButton.onClick.AddListener(() => OnClickedButton(cardIndex));
     //tempButton.GetComponentInChildren<Text>().text = "Button Numero " + cardIndex.ToString ();
     /*tempButton.onClick.AddListener(() =>
     {
         Debug.Log("IT WORKS");
         // Just handle the button clikc inside here
     });*/
     
     cardCopy.transform.position = position;

     CardModel cardModel = cardCopy.GetComponent<CardModel> ();
     cardModel.cardIndex = cardIndex;

     cardModel.ToggleFace (faceUp);

     SpriteRenderer spriteRenderer = cardCopy.GetComponent<SpriteRenderer> ();

     if (reverseLayerOrder) 
     {
         spriteRenderer.sortingOrder = 51 - positionalIndex;
     }

     else
     {
         spriteRenderer.sortingOrder = positionalIndex;
     }

     fetchedCards.Add (cardIndex, new CardView(cardCopy));
     //Debug.Log("Hand Value = " + deck.HandValue ());
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KonfuPanda · Apr 13, 2017 at 01:15 PM

I assume that your cards have a Button component.

 // card is instantiated...
 CardModel cardModel = cardCopy.getComponent<CardModel>();
 // cardmodel is initialized...
 cardCopy.getComponent<Button>().onclick.AddListener(() => DoSomethingWithCard(cardModel));
 
 //.....
 
 void DoSomethingWithCard(CardModel cardModel)
 {
   Debug.Log(cardModel.index);
   // Your code here...
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Issues after updating to Unity 5 1 Answer

Can't acces a variable from another script 1 Answer

convert unity versions, 0 Answers

How do you make a rectangle light 0 Answers

why Lagging from tilemap 0 Answers


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