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 MegaRfenixXD · Sep 01, 2018 at 04:05 AM · looplooping

Infinite Loop in void checkcard (memory game)

Class GameManager

 public Sprite[] cardface;
 public Sprite cardback;
 public GameObject[] cards;
 public Text matchText;

 private bool _init = false;
 private int  _matches = 5;

 private void Update(){
     if (!_init)
         initializeCards();

     if (Input.GetMouseButtonUp(0))
         CheckCards();
 }
 void initializeCards(){
     for(int id = 0; id < 2; id++){
         for(int i = 1; id <6; i++){

             bool test = false;
             int choice = 0;
             while(!test){
                 choice = Random.Range(0, cards.Length);
                 test = !(cards[choice].GetComponent<Card> ().inicializado);
             }
             cards[choice].GetComponent<Card>().cardValue = i;
             cards[choice].GetComponent<Card>().inicializado = true;
         }
     }


     foreach (GameObject c in cards)
         c.GetComponent<Card>().setupgraphics();

     if (!_init)
         _init = true;
 }
 public Sprite getCardBack(){
     return cardback;

 }
 public Sprite getCardFace(int i){
     return cardface [ i - 1 ];
 }




 void CheckCards() {
     List<int> c = new List<int>();

     for(int i = 0; i < cards.Length; i++){
         if (cards[i].GetComponent<Card>().state == 1)
             c.Add(i);

     }
     if (c.Count == 2)
         cardComparison(c);




 }

 void cardComparison(List<int> c){
     Card.Nao_fazer = true;

     int x = 0;

     if(cards[c[0]].GetComponent<Card>().cardValue == cards[c[1]].GetComponent<Card>().cardValue){
         x = 2;
         _matches--;
         matchText.text = "Combinações Restantes:" + _matches;
         if (_matches == 0)
             SceneManager.LoadScene("Menu");

     }
     for(int i  = 0; i < c.Count; i++){
         cards[c[i]].GetComponent<Card>().state = x;
         cards[c[i]].GetComponent<Card>().falseCheck();
     }


 }

Class Card

 public static bool Nao_fazer = false;

 [SerializeField]
 private int _state;
 [SerializeField]
 private int _cardValue;
 [SerializeField]
 private bool _inicializado = false;

 private Sprite _cardback;
 private Sprite _cardface;

 private GameObject _manager;

 private void Start()
 {
     _state = 1;
     _manager = GameObject.FindGameObjectWithTag("Manager");
 }
 public void setupgraphics()
 {
     _cardback = _manager.GetComponent<GameManager>().getCardBack();
     _cardface = _manager.GetComponent<GameManager>().getCardFace(_cardValue);

     flipCard();
 }

public void flipCard(){

     if (_state == 0)
         _state = 1;
     else if (_state == 1)
         _state = 0;


     if (_state == 0 && Nao_fazer)
         GetComponent<Image>().sprite = _cardback;
     else if(_state == 1 && !Nao_fazer)
         GetComponent<Image>().sprite = _cardface;
 }
 public int cardValue{
     get { return _cardValue; }
     set { _cardValue = value; }


 }

 public int state{
     get { return _state; }
     set { _state = value; }
 }
 public bool inicializado{
     get { return _inicializado; }
     set { _inicializado = value; }
 
 }
 public void falseCheck(){
     StartCoroutine(pause());

 }

 IEnumerator pause(){
     yield return new WaitForSeconds(1);
     if (_state == 0)
         GetComponent<Image>().sprite = _cardback;
     else if( _state == 1 )
         GetComponent<Image>().sprite = _cardface;
     Nao_fazer = false;
 }
   

}

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
1
Best Answer

Answer by Ermiq · Sep 01, 2018 at 03:23 PM

 void initializeCards() {
     for (int id = 0; id < 2; id++) {
         for (int i = 1; id < 6; i++) { // Here. 'id' is always less than 6.

Also, this while statement seems very dangerous. Are you sure it always ends up with test being true in the end? Because, as I see, this loop is always going to repeat itself.

 while(!test){
     choice = Random.Range(0, cards.Length);
     test = !(cards[choice].GetComponent<Card> ().inicializado);
 }
 cards[choice].GetComponent<Card>().cardValue = i;
 cards[choice].GetComponent<Card>().inicializado = true;
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

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

Related Questions

For loop not working as intended?!? (Beginner) 1 Answer

Mecanim animation Looping problem (Unity 4.6) 0 Answers

Is there a way to loop mp3 files seamlessly? 2 Answers

Callback from a Loop 1 Answer

Update console in the same frame 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