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 junkdog8 · Jun 18, 2015 at 04:43 AM · c#selections

multiple selection in runtime

I am attempting to make a matching game, you know where you select two face down cards, they flip over and then if they match blablabla... i am having a really agrivating problem through and i have tried to fix it over and over and i keep running into a wall. with the way i have the code setup now, when i click on 1 box it sets both selection 1 and selection 2 to the first card, then when i click on the second card it sets both selection 1 and selection 2 to the second card. i attempted to fix this by nesting the second if statement in an else statement of the first if statement, but then it only ever calls the first if statement and not the nested if statement. i am at my wits end here and could really use some assistance.

 using UnityEngine;
 using System.Collections;
 
 public class Flip : MonoBehaviour 
 {
     public GameObject gameMaster;
     public GameObject _selection1;
     public GameObject _selection2;
     private Vector3 mousePos;
     public GameObject obj;
     public bool oneSelected;
     public bool twoSelected;
 
     // Use this for initialization
     void Start () 
     {
         obj = null;
         _selection1 = null;
         _selection2 = null;
         oneSelected = false;
         twoSelected = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
 
     void OnMouseOver () 
     {
         obj = GetComponent<Collider>().gameObject;
         //Debug.Log("Hovering over: " + obj.name + " " + obj.transform.position); 
 
         if (Input.GetMouseButtonDown (0))
         {
             Debug.Log("click");
             Debug.Log ("_selection1: " + _selection1 + " " + "_selection2: " + _selection2);
             if(oneSelected == false)
             {
                 Debug.Log("_selection1 setter");
                 _selection1 = obj;
                 oneSelected = true;
                 gameMaster.GetComponent<GameMaster> ().selection1 = _selection1;
                 Debug.Log(_selection1 + "" + gameMaster.GetComponent<GameMaster> ().selection1);
             }
             if(oneSelected == true && twoSelected == false)
             {
                 Debug.Log("_selection2 setter");
                 _selection2 = obj;
                 twoSelected = true;
                 gameMaster.GetComponent<GameMaster> ().selection2 = _selection2;
                 Debug.Log(_selection2 + "" + gameMaster.GetComponent<GameMaster> ().selection2);
             }
 
             Debug.Log ("_selection1: " + _selection1 + " " + "_selection2: " + _selection2);
         }
     }
 
     void reset()
     {
         //oneSelected = null;
         //twoSelected = null;
     }
 }


and if for some reason this is effecting it, here is the gamemaster script.

 using UnityEngine;
 using System.Collections;
 
 public class GameMaster : MonoBehaviour 
 {
     public GameObject selection1;
     public GameObject selection2;
 
     // Use this for initialization
     void Start ()
     {
         Debug.Log ("Selection1: " + selection1 + "Selection2: " + selection2);
         selection1 = null;
         selection2 = null;
     }
     
     // Update is called once per frame
     void Update () 
     {
         //Debug.Log ("Selection1: " + selection1 + "Selection2: " + selection2);
     }
 }
 

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

Answer by maccabbe · Jun 18, 2015 at 05:24 AM

Currently your code is working like this


1) oneSelected is false

2) if condition on line 39 is true

2a) code sets oneSelected to true

2b) first card selected

3) if condition on line 47 checks if oneSelected is true, which it is since 2a just set it to true

3a) code sets twoSelected to true

3b) second card selected


As you can see, your code immediately choses both cards. Use an if/elseif/else structure to avoid this and select one card at a time.

https://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements

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
avatar image
0

Answer by junkdog8 · Jun 18, 2015 at 11:26 PM

I actually figured out what was wrong and facepalmed hard. I had this script on each card and so was calling each card separately as I hovered over it.

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

Variable won't update (C#, Unity) 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