Search Unity

problem with and statement or some other issue

Discussion in 'Scripting' started by 2dgamemania, Oct 24, 2014.

  1. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    I'm looking for some help with the below code. The Debug shows the output fine for activecarda and activecardb but the problem is with the bottom section of the code, I cannot get it to show "matchfound". I tried using && statement instead of nesting if statements, same issue. any ideas what i'm doing wrong?


    if (this.tag=="card1")
    {
    activecarda=1;
    Debug.Log("activecarda "+activecarda);
    }

    if (this.tag=="card9")
    {
    activecardb=9;
    Debug.Log("activecardb "+activecardb);
    }

    //this is the problem below, doesnt print matchfound, tried using && loop but same issue

    if (activecarda==1)
    {
    if (activecardb==9)
    {
    Debug.Log("matchfound");
    }
    }
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Please use code tags.
    Code (CSharp):
    1. if (activecarda==1) {
    2.      if (activecardb==9) {
    3.           Debug.Log("matchfound");
    4.      }
    5. }
    The code looks fine, maybe activecarda & activecardb aren't set to the correct values for it to debug the match?

    Can you should me the console when you try to do this?
    Like, make sure that both cards are activated... o_o
     
  3. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    The debug log shows values 1 for activecarda and 9 for activecardb. I tried your code above and same issue, doesnt print matchfound

    I'm using this code within function OnMouseDown() , could this be part of the problem.
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    The code should be working.
    Make the booleans public, and see if they are both true in the unity inspector.
     
  5. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    made sure the variables were public, added a screenshot of the debug to show it does show the values of the 2 cards ok..
     

    Attached Files:

  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Is all the code you pasted in the same function? Are activecarda and activecardb static? Can you show us the whole class? (Use the CODE tags!) I have a feeling the error is not in the code you have shown us but somewhere else.
     
  7. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    Code below, I am using android format, could it be anything to do with that, I dont get any errors though....


    Code (csharp):
    1.  
    2.  
    3.  
    4. function OnMouseDown () {
    5.  
    6.    if (this.tag=="card1")
    7.    {
    8.    animation.Play("anim3");
    9.    activecarda=1;
    10.    Debug.Log("activecarda "+activecarda);
    11.    }
    12.    
    13.    if (this.tag=="card9")
    14.    {
    15.    animation.Play("anim3");
    16.    activecardb=9;
    17.    Debug.Log("activecardb "+activecardb);
    18.    }
    19.    
    20.    
    21.    if (activecarda==1)
    22.      {
    23.        if (activecardb==9)
    24.          {
    25.            Debug.Log("matchfound");
    26.            //print ("escape pressed");
    27.            //Application.Quit();
    28.          }
    29.      }
    30.  
    31. }
    32.  
     
  8. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    I've manged to get it working, it was to do with the public variable, I thought I done it right by putting

    Code (csharp):
    1.  
    2. public var activecarda
    3.  
    but had to put

    Code (csharp):
    1.  
    2. static var activecarda
    3.  

    I think I'll have to read up on how these variables work within functions as I have other variables declared which work fine within other functions which were declared the same way as activecarda and activecardb

    Thanks for your help, much appreciated.
     
  9. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Yeah, so you've attached the same script on different objects, just like @Garth Smith suggested.
    The problem the two scripts had different booleans.
    So one of the two booleans would always be false in any instance of the script.
    Changing it to a static variable made it so the variable is shared over all instances.
    People often use a manager script to save values that are supposed to be shared.
    You'll avoid making this mistake doing so, because there's only supposed to be 1 instance of a manager script.