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

How to take away 1 from buildindex?

Discussion in 'Scripting' started by j7stin, Feb 11, 2020.

  1. j7stin

    j7stin

    Joined:
    Jan 7, 2020
    Posts:
    22
    Good day everyone,

    My game's 'scenes in build' are configured like this:
    upload_2020-2-11_18-0-2.png

    I have text in every scene as part of a canvas that tells the player what level they are on.
    When I'm on level1 (scene 2), I want this text to say level: followed by 1.
    Right now, I have this
    Code (CSharp):
    1. levelText.text = "Level: " + SceneManager.GetActiveScene().buildIndex;
    Now of course, when I'm on level 1, this will just display 2 as the buildindex is 2.
    I can do
    Code (CSharp):
    1. levelText.text = "Level: " + SceneManager.GetActiveScene().buildIndex + 1;
    That works fine and it comes out with 3.
    When I replace the +1 with -1, it gives me this error

    upload_2020-2-11_18-3-32.png
    That is literally it.

    I do not understand why this doesn't work. It may be a stupid question but I beg someone helps me.

    I understand the error it's telling me, I think the BuildIndex is coming out as a string and the -1 is taking away an integer which shouldn't work but how come it works when I do +1?

    Thanks,
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Put parentheses around (SceneManager.GetActiveScene().buildIndex - 1). That'll treat that as int math, before combining with the string.

    (I'm actually quite surprised that your example with + DOES work - I would fully expect it to show "Level: 21" if you're on scene 2.)
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'm also surprised to see that behaviour (although I would not have expected a "Level 21" (if you are on scene 2 as StarManta suggested)), I would have expected the same error from the compiler because it is ambiguous. I can only surmise that this is because of some special treatment that '+' receives: "+" is overloaded for strings, '-' isn't (which is probably why StarManta expected the result to be '21'). Checking the operator precedence table also doesn't clear this up, so I'm guessing that this is simply an imbalance that Unity left in there: the code is smart enough to check if a '+' is followed by the same type to wait with the implicit ToString(), but fails to do so for '-'.

    Strange indeed.

    [Edited for incorrectly quoting Star Manta. My apologies]
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    He says buildIndex is 2, not 1.
     
  5. j7stin

    j7stin

    Joined:
    Jan 7, 2020
    Posts:
    22
    Thank you so much for helping out :)
    It was pretty simple but I just didn't know. Love the community on Unity so far, helped me out so much as a beginner lmao :D