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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Refrencing two int's in an if statement ?

Discussion in 'Scripting' started by Endzone, Mar 10, 2016.

  1. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hello so im having a little bit of a problem at the moment im using this,

    Code (CSharp):
    1. if(voxelInfo.chunk.GetVoxel (indexUp) == 40){
    2.                     PlaceCornerLeftUp();
    3.                 }
    4.                 if(voxelInfo.chunk.GetVoxel (indexUp) == 39){
    5.                     PlaceCornerLeftUp();
    6.                 }
    Im having to use that little bit of code a fair amount of times through out my script and as they are the exact same thing with just two seperate Ints i was wondering is their a way i can refrence the two int's 39 and 40 at the same time in one If statement rather then have to use two statements everytime i want to do the above code ??
    - thanks in advance if anyone can help, If my question wasn't clear enough just let me know and i will explain more indepth
     
  2. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    I may be misunderstanding, but if you're just wanting to check the condition of two values in a single statement use the || operator. This is the "or" operator in C# (and many other languages) and allows you to combine conditional checks into a single statement.

    Example:
    Code (CSharp):
    1. if(voxelInfo.chunk.GetVoxel (indexUp) == 40 || voxelInfo.chunk.GetVoxel (indexUp) == 39)
    2. {
    3.     PlaceCornerLeftUp();
    4. }
     
  3. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Ohh i see, Thank you very much Great help :D Was exactly what i needed!
     
  4. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Good to hear!
     
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Kiwasi likes this.
  6. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hey, thanks i will for sure check it out i have been coding for almost a year and a half now but their is still so much i dont know or have never used before such as "||" so that will be really helpful :)