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 MakeITeasy · Nov 16, 2013 at 06:57 PM · javascriptcardsplanes

How to do the matches in a memory card game?

Hi everyone! I'm making a memory card game. I made 2 planes and parenting them together so they make a single card,i made another one too. Then, i added a common back side texture from gui for both cards. Every card on Start() gets randomly its front texture like below (javascript)(on StartUp.js):

 public var oPlane: GameObject;
 
 function Start () {
 var texturesCollection: Object[] = Resources.LoadAll("Front_Face_cards");
 var range_1 = Random.Range(0,texturesCollection.length);
 
 if(oPlane.Find("front_card_1") && oPlane.Find("front_card_2")){
     oPlane.Find("front_card_1").renderer.material.mainTexture = texturesCollection[range_1];
     oPlane.Find("front_card_2").renderer.material.mainTexture = texturesCollection[range_1];
 }
 }

Now, on each plane i attached this code on (Flip_card.js):

 public var card_1: GameObject;
 card_1 = GameObject.Find("card_1");
 
 function OnMouseDown () {
     card_1.transform.localRotation.z = 180;
     CheckCards();
 }

So that, every time i clicked on a card this card can flip (the camera shows from above). Later on, i check for any similarities with those 2 cards by calling CheckCards(), which is below in detail (Flip_card.js continue):

 function CheckCards(){
 
 if((card_1.transform.localRotation.z == 180) && (card_2.transform.localRotation.z == 180 )){
         if(card_2.renderer.material.mainTexture == card_1.renderer.material.mainTexture){
             Destroy(card_1);
             Destroy(card_2);
         }
     }

Unfortunately, it's not working. i can't destroy my two cards after i flip them both. I stuck there, i checked most of the answers close to my problem here but no luck. Where do i check if my cards are the same and then destroy them? On the script attached to one or both cards, or on Update(), or on Start()? Thanx in advance!

(UPDATED)

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

2 Replies

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

Answer by Nition · Nov 16, 2013 at 07:09 PM

I just tested this and your texture comparison code definitely works, so something else is wrong somewhere. I'd suggest removing the localRotation check temporarily and seeing if it works with just the texture comparison, if you haven't done that already.

If the texture comparison is the broken part, is it possible that your material has multiple textures, or that you're not checking the correct renderer?

Comment
Add comment · Show 4 · 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 MakeITeasy · Nov 16, 2013 at 07:52 PM 0
Share

if i remove the localRotation check what it happens is: when i click on one of two similar cards they disappear (which it works in a way) but i can't see the flipping of the card though. i click on the one and they both get the destroy effect without seeing the flip effect ( localRotation.z =180) or click on the other card as well to see if they are both the same. thanks anyway. maybe i have to put my check on somewhere else, like on the Update()?

avatar image Nition · Nov 16, 2013 at 09:12 PM 0
Share

With the current code, you'll never see them flip because the rotation and the check happen on the same frame. There are multiple ways you could solve it but a simple one is set the rotation when you click like you do now, but call the CheckCards method with Invoke: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html

That way you can delay the check until the card has shown for a second, or whatever.

avatar image MakeITeasy · Nov 17, 2013 at 09:19 AM 0
Share

That's it. At least it works as i want it for now. I was trying to find a similar statement, like in C#, yield return new WaitForSeconds(5); for js. Thanx for the help!

avatar image Nition · Nov 17, 2013 at 09:25 AM 0
Share

That's a coroutine. You CAN do it with a coroutine in JavaScript as well, but in this case an Invoke is easier. You were on the right track though.

avatar image
0

Answer by tw1st3d · Nov 16, 2013 at 07:42 PM

So, I kind of forgot what I was doing while writing this, so it's probably incomplete. This is a C# solution for handling and comparing cards. Might be a little over-doing it, but it's not throwing any errors (in the inspector, haven't bothered testing it in a scene.)

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 
 public class CardHandler : MonoBehaviour
 {
     private int maxCards = 0;                                     // Define yourself.
     private List<GameObject> cards = new List<GameObject>();    // Leave this
     private List<Card> cardHandlers = new List<Card>();            // Leave this
     private GameObject card1, card2;                            // Leave these
     
     private void Start()
     {
         generateCards();
     }
     
     private void generateCards()
     {
         float x = 0, y = 0, z = 0;                 // Define these based on table layout.
         float cardWidth = 0, cardHeight = 0;     // Define these based on your models size.
         
         for(int i = 0; i < maxCards; i++) 
         {
             Card newCard = new Card();
             cards.Add(freshCard(newCard, x, y, z));
             x += cardWidth + 20;
             y += cardHeight + 20;
         }
     }
     
     private GameObject freshCard(Card C, float x, float y, float z)
     {
         System.Random rand = new System.Random();
         GameObject newCard = Instantiate(C.card, new Vector3(x,y,z), C.card.transform.rotation) as GameObject;
         
         int cID = rand.Next(0, C.materials.Length);
         Material toSetMat = C.materials[cID];
         
         newCard.renderer.material = toSetMat;
         
         return newCard;
     }
     
     public void Update()
     {
         /* Your Click Logic */
         GameObject clickedObject = null;
             
         if(clickedObject) 
         {
             // If you've clicked a card
             foreach(Card k in cardHandlers) 
             {
                 // iterate through cards
                 if(clickedObject == k.card) 
                 {
                     // If the current card is the one you clicked
                     if(k.isFlipped) 
                     {
                         // If it's already flipped, ignore the click
                         continue;
                     } else { 
                         // Else flip it
                         k.isFlipped = true;
                         if(card1) 
                         {
                             // If this is not the first card flipped
                             if(card2) 
                             {
                                 // If this is not the second card flipped, ignore click
                                 continue;
                             } else { 
                                 // Else set card two to clicked card
                                 card2 = k.card;
                                 if(card1 == card2)
                                 {
                                     /* Your logic on correct match */
                                 } else {
                                     /* Your logic on incorrect match */    
                                 }
                             }
                         } else { 
                                 // Else set card one to clicked card
                             card1 = k.card;    
                         }
                     }
                 } else { 
                     // Ignore all other cards
                     continue;    
                 }
             }
         }
         
     }
 }
 
 public class Card : Component
 {
     public GameObject card;         // Define with your default card model. No material.
     public Material[] materials;     // Define with your material list.
     private int cardID;                // Defines themselves.
     public bool isFlipped = false;    // Leave this
 }
Comment
Add comment · Show 1 · 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 MakeITeasy · Nov 16, 2013 at 08:50 PM 0
Share

Though it's C#, it's a good example for what i want, but in case of 20 cards (which my next levels would be with 20 cards for example) it would be more complicated as for the if-else statements for each card.

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

20 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Not sure if you can do this.. 2 Answers

UnityScript, Delegates and SmoothMoves 1 Answer

'text' is not a member of 'OBJECT' JavaScript Not Working in Unity 3.5 - Lost Commands? 2 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