Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need Help For logic input on C#

Discussion in 'Scripting' started by Paulo-Melo, Jun 21, 2016.

  1. Paulo-Melo

    Paulo-Melo

    Joined:
    Jun 2, 2016
    Posts:
    29
    Hello,

    My game is a penalty kick based game, and i want to set active my button after 2 kick (one for me and one for CPU), after 4 kicks, 6 kicks, till 10 kicks (5 for me and 5 for cpu) but i dont know how can i put that in code..
    I already have tried: if (_count / 2 =1, 2, 3, 4, 5) but it gives me parsing error

    My current code is:

    Code (CSharp):
    1. void NextTurn()
    2. {
    3.         if (_count / 2 >=1)
    4.         {
    5.             MyButton.Setactive(true);
    6.     {
    7.      
    8.   if (_count / 2 >= maxTurn)
    9.         {
    10.             Finalize();  
    11.            
    12.         }
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Normally you would separate the state for each player. You could then determine whose turn it is with subtraction.

    Pseudocode:
    Code (CSharp):
    1. public class Player
    2. {
    3.    public int Kicks;
    4. }
    Code (CSharp):
    1. if ((CPU.Kicks + Player.Kicks) >= MaxKicks)
    2. {
    3.    //end the game
    4. }
    5.  
    6. int k = (CPU.Kicks - Player.Kicks)
    7. if (k > 0)
    8. {
    9.    //players turn
    10. }
    11. else if (k < 0)
    12. {
    13.    //CPUs turn
    14. }
     
    Last edited: Jun 21, 2016
    Paulo-Melo likes this.
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Ah, you want to only have your button active, if the count is even.

    Code (csharp):
    1.  
    2. void NextTurn(){
    3.     MyButton.SetActive(_count & 1 == 0);
    4. }
    5.  
     
  4. Paulo-Melo

    Paulo-Melo

    Joined:
    Jun 2, 2016
    Posts:
    29
    Yes, but i dont want it to appear in the first kick..
     
  5. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    This seems like pretty much what you are looking for:

    Code (CSharp):
    1. if (_count==2 || _count==4 || _count==6 || _count==8 || _count==10)
    These other ones might work also:

    Code (CSharp):
    1. if (_count>0 && _count%2==0)
    Code (CSharp):
    1. if (_count>0 && _count&1==0)
     
    Paulo-Melo likes this.
  6. Paulo-Melo

    Paulo-Melo

    Joined:
    Jun 2, 2016
    Posts:
    29
    boolfone i´ve tried
    Code (CSharp):
    1.     if (_count>0 && _count%2==0)
    2.  
    and it works !!
    Thank you very much !! :D :D
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ooo, this puzzled me to start with, but am I right in thinking this is using "bitwise and" to check for odd/even? hadn't thought of doing it that way and it's quite elegant :)
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    yeah, that one is bit wise, but the modulus approach is also valid, so if you wanted every 3rd click, then I would use that, but specifically, the OP wanted every even.