Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

  private void setOption(string optionKey, bool optionBoolValue) {
   //convert bool to int
   int optionIntValue;
   if (optionBoolValue == true) {
    optionIntValue = 1;
   }
   else {
    optionIntValue = 0;
   }

   //set the Player Prefis Value
   PlayerPrefs.SetInt ("options_" + optionKey,optionIntValue);
   PlayerPrefs.Save();

   //I want to do this but getting a null reference exception for "this" argh!!
   //GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
   //GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);

   if(optionKey == "use_0") {
    this.use_0 = optionBoolValue;
    this.previous_use_0 = optionBoolValue;
   }
   if(optionKey == "play_sounds") {
    this.play_sounds = optionBoolValue;
    this.previous_play_sounds = optionBoolValue;
   }
  }