Search Unity

Player Prefs Error - C#

Discussion in 'Scripting' started by Jana1108, Nov 28, 2015.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi, in my game I want to make it so that the playerprefs "leveltoload" will go up a level each time they collide with the objective in different scenes but I'm getting an error.

    Error:
    Assets/Scripts/LevelUp.cs(18,100): error CS1525: Unexpected symbol `;', expecting `)', or `,'

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelUp : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     public GameObject objective;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     void OnTriggerEnter (Collider other) {
    15.        
    16.         if(other.gameObject.tag == "Player")
    17.         {
    18.             PlayerPrefs.SetInt ("levelToload", (PlayerPrefs.GetInt ("levelToload") + 1);
    19.             Application.LoadLevel ("NextLvl");
    20.         }
    21.        
    22.     }
    23.  
    24. }
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    It's a compiler error that is explaining that you have an extra opening parenthases in your line 18. All parenthases must come in matched pairs.

    Errors like this are why I prefer to keep every line short. Nesting statements can quickly get confusing.

    I would suggest rewriting the inner block to this:
    Code (csharp):
    1.  
    2. const string levelLoadKeyName = "levelToload";
    3. int levelToLoad = PlayerPrefs.GetInt(levelLoadKeyName);
    4. levelToLoad += 1;
    5. PlayerPrefs.SetInt(levelLoadKeyName, levelToLoad);
    6.  
    This is more verbose, but it is also very clear and easy to read. Making code clear and easy to read is a fundamental part of Clean Clode.
     
  3. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Thanks so much! Turns out all I had to do to get it working was add that extra parenthesis at the end of line 18.
     
  4. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Also would anyone know about when the scenes in my game change the lighting goes really dark and loses most of it's color. Can anyone help me fix this please?
     
  5. FeedorFeed

    FeedorFeed

    Joined:
    Jan 9, 2014
    Posts:
    35
    You probably need to an an extra directional light for the extra scene?
     
  6. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    There's directional lights in all of the scenes. It doesn't happen if I just play the scene by itself but say if I go to main menu and click play then the play scene will be dark whereas if I just went into the play scene and clicked play it would appear normal.