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!!! IF STATEMENTS

Discussion in 'Scripting' started by RealAspireGames, Jun 26, 2014.

  1. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Hello
    I am currently working on a game, And i am having trouble with my IF statements So here is the objective the player needs to collect 4 packages for something else to happen, I am trying this

    Code (JavaScript):
    1.     if(ecrate1collected == true)
    2.     if(ecrate2collected == true)
    3.     if(ecrate3collected == true)
    4.     if(ecrate4collected == true)
    5.     {
    6.     repairshipGui = true;
    7.     }
    But every time i pick up the first E-Crate it loads the next level, I need the player to collect all 4 before they move on to the next level.

    PLEASE HELP!
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1. if (ecrate1collected && ecrate2collected && ecrate3collected && ecrate4collected)
    2. {
    3.     Potato!;
    4. }
    &&; is the logic operator "And"
    ||; is the logic operator "Or"

    Boolean does not need to be tested against true/false, you can do "if (myBoolean)" or "if (!myBoolean)
     
  3. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Its weird i put in the && Statements and its still loading the next level every time i pick up the very first E-Crate!

    Thanks for your help tho
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Post more code...

    Where is repairshipGui set? Where is it used? What set the ecrateXcollected values?
     
  5. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Your code is definitely better, @LightStriker, but I think it's still functionally equivalent to the OP's.

    @godofwarfare115: The problem isn't in your code snippet. It's somewhere else. Are you sure you're not settings all those variable to true somewhere else in your code?
     
  6. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Yea, I'm using a custom built crafting system, Here is the full code

    Code (CSharp):
    1. #pragma strict
    2.  
    3. var logCollected : boolean = false;
    4. var plankscollected : boolean = false;
    5. var firewoodcollected : boolean = false;
    6. var commpeicecollected : boolean = false;
    7. var stonecollected : boolean = false;
    8. var stickscollected : boolean = false;
    9. var gunpowdercollected : boolean = false;
    10. var metalcollected : boolean = false;
    11.  
    12. //E-CRATES TO FINISH THE SHIP BUILD
    13. var ecrate1collected : boolean = false;
    14. var ecrate2collected : boolean = false;
    15. var ecrate3collected : boolean = false;
    16. var ecrate4collected : boolean = false;
    17.  
    18. private var showGui : boolean = false;
    19. private var plankGui : boolean = false;
    20. private var fireGui : boolean = false;
    21. private var commGui : boolean = false;
    22. private var axeGui : boolean = false;
    23. private var bulletGui : boolean = false;
    24. private var repairshipGui : boolean = false;
    25.  
    26. var plank : Transform;
    27. var player : Transform;
    28. var basicshelter : Transform;
    29. var fireplace : Transform;
    30. var commtower : Transform;
    31. var craftedaxe : Transform;
    32. var craftbullets : Transform;
    33. var repairship : Transform;
    34.  
    35. //E-CREATES SPAWN LOCATIONS!!!
    36. var espawn1 : GameObject;
    37. var espawn2 : GameObject;
    38. var espawn3 : GameObject;
    39. var espawn4 : GameObject;
    40.  
    41. // E-CRATES TRANSFORMS TO SPAWN!!!
    42. var ecrate1 : Transform;
    43. var ecrate2 : Transform;
    44. var ecrate3 : Transform;
    45. var ecrate4 : Transform;
    46.  
    47. function Update()
    48. {
    49.     if(logCollected == true)
    50.     {
    51.         showGui = true;
    52.     }
    53.    
    54.     if(plankscollected == true)
    55.     if(stickscollected == true)
    56.     {
    57.       plankGui = true;
    58.     }
    59.     if(firewoodcollected == true)
    60.     if(stonecollected == true)
    61.     {
    62.      fireGui = true;
    63.     }
    64.     if(commpeicecollected == true)
    65.     {
    66.      commGui = true;
    67.     }
    68.     if(stonecollected == true)
    69.     if(stickscollected == true)
    70.     {
    71.      axeGui = true;
    72.     }
    73.     if(metalcollected == true)
    74.     if(gunpowdercollected == true)
    75.     {
    76.     bulletGui = true;
    77.     }
    78.     if (ecrate1collected && ecrate2collected && ecrate3collected && ecrate4collected)
    79.     {
    80.     repairshipGui = true;
    81.     }
    82.    
    83.     //CODE BELOW IS FOR ACTUALLY CRAFTING THE ITEM!
    84.    
    85.     if(logCollected == true && Input.GetKey("p"))
    86.     {
    87.         MakePlank();
    88.     }
    89.     if(stickscollected == true)
    90.     if(plankscollected == true && Input.GetKey("p"))
    91.     {
    92.       MakeShelter();
    93.     }
    94.     if(firewoodcollected == true)
    95.     if(stonecollected == true && Input.GetKey("p"))
    96.     {
    97.       MakeFire();
    98.     }
    99.     if(commpeicecollected == true && Input.GetKey("p"))
    100.     {
    101.      placecommtower();
    102.      spawnecrates();
    103.     }
    104.     if(stonecollected == true)
    105.     if(stickscollected == true && Input.GetKey("p"))
    106.     {
    107.      craftaxe();
    108.     }
    109.     if(metalcollected == true)
    110.     if(gunpowdercollected == true && Input.GetKey("p"))
    111.     {
    112.     makebullets();
    113.     }
    114.     if (ecrate1collected && ecrate2collected && ecrate3collected && ecrate4collected && Input.GetKey("p"))
    115.     {
    116.      spaceshiprepair();
    117.     }
    118. }
    119.  
    120. function OnGUI()
    121. {
    122.     if(showGui == true)
    123.     {
    124.         GUI.Box(new Rect(200, 200, 200, 25), "Press P to craft your plank");
    125.     }
    126.     if(plankGui == true)
    127.     {
    128.       GUI.Box(new Rect(200, 200, 200, 25), "Press P to craft Basic Shelter");
    129.     }
    130.     if(fireGui == true)
    131.     {
    132.      GUI.Box(new Rect(200, 200, 200, 25), "Press P to place FirePit");
    133.     }
    134.     if(commGui == true)
    135.     {
    136.      GUI.Box(new Rect(300, 300, 300, 30), "press P to place comm tower");
    137.     }
    138.     if(axeGui == true)
    139.     {
    140.      GUI.Box(new Rect(200, 200, 200, 25), "Press P to craft Axe");
    141.     }
    142.     if(bulletGui == true)
    143.     {
    144.     GUI.Box(new Rect(200, 200, 200, 25), "Press P to make Bullets");
    145.     }
    146.     if(repairshipGui == true)
    147.     {
    148.     GUI.Box(new Rect(200, 200, 200, 25), "Press P To Finish Chapter 1");
    149.     }
    150. }
    151.  
    152. function MakePlank()
    153. {
    154.     Instantiate (plank, player.transform.position, Quaternion.identity);
    155.     logCollected = false;
    156.     showGui = false;
    157. }
    158.  
    159. function MakeShelter()
    160. {
    161. Instantiate (basicshelter, player.transform.position, Quaternion.identity);
    162.     logCollected = false;
    163.     plankscollected = false;
    164.     plankGui = false;
    165.     stickscollected = false;
    166. }
    167.  
    168. function MakeFire()
    169. {
    170. Instantiate (fireplace, player.transform.position, Quaternion.identity);
    171.     logCollected = false;
    172.     plankscollected = false;
    173.     firewoodcollected = false;
    174.     fireGui = false;
    175.     stonecollected = false;
    176. }
    177. function  placecommtower()
    178. {
    179. Instantiate (commtower, player.transform.position, Quaternion.identity);
    180.     logCollected = false;
    181.     plankscollected = false;
    182.     firewoodcollected = false;
    183.     fireGui = false;
    184.     commGui = false;
    185.     commpeicecollected = false;
    186.    
    187. }
    188. function craftaxe()
    189. {
    190. Instantiate (craftedaxe, player.transform.position, Quaternion.identity);
    191.     logCollected = false;
    192.     plankscollected = false;
    193.     firewoodcollected = false;
    194.     fireGui = false;
    195.     commGui = false;
    196.     commpeicecollected = false;
    197.     stonecollected = false;
    198.     axeGui = false;
    199. }
    200.  
    201. function makebullets()
    202. {
    203. Instantiate (craftbullets, player.transform.position, Quaternion.identity);
    204.     logCollected = false;
    205.     plankscollected = false;
    206.     firewoodcollected = false;
    207.     fireGui = false;
    208.     commGui = false;
    209.     commpeicecollected = false;
    210.     stonecollected = false;
    211.     axeGui = false;
    212.     bulletGui = false;
    213.     gunpowdercollected = false;
    214.     metalcollected = false;
    215. }
    216.  
    217. function spaceshiprepair()
    218. {
    219.     logCollected = false;
    220.     plankscollected = false;
    221.     firewoodcollected = false;
    222.     fireGui = false;
    223.     commGui = false;
    224.     commpeicecollected = false;
    225.     stonecollected = false;
    226.     axeGui = false;
    227.     bulletGui = false;
    228.     gunpowdercollected = false;
    229.     metalcollected = false;
    230.     ecrate1collected = false;
    231.     ecrate2collected = false;
    232.     ecrate3collected = false;
    233.     ecrate4collected = false;
    234.     repairshipGui = false;
    235.     Application.LoadLevel ("credits");
    236. }
    237. function spawnecrates()
    238. {
    239. Instantiate (ecrate1, espawn1.transform.position, Quaternion.identity);
    240.   Instantiate (ecrate2, espawn2.transform.position, Quaternion.identity);
    241.    Instantiate (ecrate3, espawn3.transform.position, Quaternion.identity);
    242.     Instantiate (ecrate4, espawn4.transform.position, Quaternion.identity);
    243. }
     
  7. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Just Posted the entire code!
     
  8. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Can Anyone Help me fix this issue?
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Where is the code that actually put ecrate4collected to true?
     
  10. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Ok, I will put that one to i'm using the var ManageScript : ManageInv code to call eachother when needed
     
  11. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Here is the e-crate 4 code
    Code (CSharp):
    1. #pragma strict
    2.  
    3. private var mouseOver = false;
    4.  
    5. var lengthBar : int = 150;
    6. var widthBar : int = 25;
    7.  
    8. function OnMouseOver()
    9. {
    10.     mouseOver = true;
    11. }
    12.  
    13. function OnMouseExit()
    14. {
    15.     mouseOver = false;
    16. }
    17.  
    18. function OnGUI()
    19. {
    20.     if(mouseOver == true)
    21.     {
    22.         GUI.Box(new Rect(400, 400, lengthBar, widthBar), "Press E to pick up E-Create #4");
    23.     }
    24.     if(Input.GetKey("e"))
    25.     {
    26.     Destroy(gameObject);
    27.    
    28.     var ManageScript : ManageInv = GameObject.Find("CraftSpawn").GetComponent(ManageInv);
    29.    
    30.     ManageScript.ecrate4collected = true;
    31.     }
    32. }
     
  12. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Well, I have no idea. I would probably need to add logs around or some break point to see what's going wrong.
     
  13. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Yea, Its really weird... Thanks for the help tho
     
  14. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Apparently when you collect a crate it sets all 4 variables to true.
     
  15. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    I bet you have similar code for crates 1-3? Aside from some seriously unnecessary code duplication, your code does not require that you be hovering over a crate to pick it up. Pressing 'e' anywhere in the scene will set ecrate4collected to true, and I'm guessing it will simultaneously do the same for the other crates.

    For a quick fix try putting the if(Input.GetKey('e')) block inside of the if(mouseover == true) block for all 4 crate scripts. You could definitely use some refactoring though. Look into arrays or other containers such as Lists, as generally anytime you need variables enumerated like you have, you should transition them to an array of some sort.
     
    jister likes this.
  16. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Problem has been fixed, thank you all for your support :)

    Turns out i had to use a function on the pickup script that will set that certain crate to true when the player picks it up..

    Thanks again to everyone who helped
     
  17. MCoburn

    MCoburn

    Joined:
    Feb 27, 2014
    Posts:
    71
    Also consider using switch statements, but that's C# I think...