0

Me and my friend have been hacking at it for hours but we just can't figure out what's wrong with it. It's essentially running through an array and if the button should be locked or interactable, and if it's null it'll be interactable. By using player prefs these settings should persist through each session with the app.

Here's the code:

for (i = 0; i < buttons.Length; i = i + 1) {

    if (PlayerPrefs.GetInt("button" + string.Format i) == null) {

        PlayerPrefs.SetInt("button" + string.Format i, 1);
    }

    if (PlayerPrefs.GetInt("button" + string.Format i) == 1) {

        button.interactable = true;

    } else {

        button.interactable = false;

    }
}

Currently unity is displaying 5 errors:

  • error CS1525: Unexpected symbol `i' (2 of these)
  • error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
  • error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
  • error CS8025: Parsing error
2
  • i = 0; line should be int i = 0; that's what's causing the symbol error. Commented Oct 22, 2016 at 23:24
  • Tiny improve: change "i = i + 1" to "i++" Commented Oct 22, 2016 at 23:25

1 Answer 1

5

Just a guess, but you should write:

for (int i = 0; i < buttons.Length; ++i) {

You maybe forgot to declare i

Also this line:

PlayerPrefs.GetInt("button" + string.Format i)

string.Format is a static method of string. The syntax is wrong. You can write it this way:

PlayerPrefs.GetInt("button" + i)

Or this way:

PlayerPrefs.GetInt(string.Format("button{0}",i));
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you this solved my problem :D Sorry I'm new to C# so I don't know all of the syntax yet for strings etc.
Sure thing! :) I'm glad I could help!
Ah, and if it helped you, please mark this post as an answer :) Clicking on the check mark
++i needs to be i++
it makes no difference in a for loop, since the increment is a single operation instruction. makes sense using it like: var value = ++i;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.