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

Unexpected symbol 'case'

Discussion in 'Scripting' started by valeriaC, Aug 13, 2015.

  1. valeriaC

    valeriaC

    Joined:
    Apr 8, 2014
    Posts:
    4
    Hello,
    I have a little problem with my code. It said unexpected symbol 'case' but i don't understand why. If someone can help me..
    it's a pretty long code but that's where the problem lies :

    }
    break;
    caseDirection.RIGHT:
    if (snakePos[0].x < 982)
    {
    //wecanmoveright
    snakePos[0] = newRect(snakePos[0].x + 20, snakePos[0].y, snakePos[0].width, snakePos[0].height);

    //nowupdatetherestofourbody
    UpdateMovePosition(tempRects);

    //checkforfood
    if (CheckForFood() == true)
    {
    //checkforvalidbuildsegmentpositionandaddasegment

    //createatemporarycheckposition (thisoneisleftofthelastsegmentinsnakePos[])
    segmentRect = CheckForValidLeftPosition();
    if (segmentRect.x != 0)
    {

    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }
     
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    From here its hard to tell, because of the formatting of your code, but more than likely, if you already have a Switch set up, is you would need a space between

    Code (csharp):
    1. case Direction.RIGHT:
    Also, what is Direction? Is that a Transforms direction?
     
  3. valeriaC

    valeriaC

    Joined:
    Apr 8, 2014
    Posts:
    4
    oh .. I don't really know .. this is the first time I write a code.
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Is "Direction" an enum or something?
     
  5. valeriaC

    valeriaC

    Joined:
    Apr 8, 2014
    Posts:
    4
    usingUnityEngine;
    usingSystem.Collections;
    usingSystem.Collections.Generic;

    publicclassSnake : MonoBehaviour
    {
    //privatestaticinstanceofclass
    privatestaticSnakeinstance = null;
    //privatefields
    privateList<Rect> snakePos = newList<Rect>();
    privateList<Texture2D> snakeIcon = newList<Texture2D>();
    privateintsnakeLength = 2;
    privatefloatmoveDelay = 0.5f;
    privateAudioClipmove1;
    privateAudioClipmove2;
    privateAudioClipdeath;

    //directionenumforclarification
    publicenumDirection
    {
    UP,
    DOWN,
    LEFT,
    RIGHT
    //---------------------------------------------------------------------------------------------
    //constructorfield: Instance
    //---------------------------------------------------------------------------------------------
    //CreatesaninstanceofSnakeifonedoesnotexists
    //---------------------------------------------------------------------------------------------

    publicstaticSnakeInstance
    {
    get
    {
    if (instance == null)
    {
    instance = newGameObject("Snake").AddComponent<Snake>();
    }

    returninstance;
    }
    }

    //---------------------------------------------------------------------------------------------
    //Unitymethod: OnApplicationQuit()
    //---------------------------------------------------------------------------------------------
    //Calledwhenyouquittheapplicationorstoptheeditorplayer
    //---------------------------------------------------------------------------------------------
    publicvoidOnApplicationQuit()
    {
    DestroyIntance();
    }

    //---------------------------------------------------------------------------------------------
    //DestroyInstance()
    //---------------------------------------------------------------------------------------------
    //DestroystheSnakeinstance
    //---------------------------------------------------------------------------------------------
    publicvoidDestroyInstance()
    {
    print("Snake Instance destroyed");

    instance = null;
    }

    //---------------------------------------------------------------------------------------------
    //Start()
    //---------------------------------------------------------------------------------------------
    //UnityMonoBehaviorcall, runsautowhenobjectiscreated, usedforinitialization
    //---------------------------------------------------------------------------------------------
    voidStart ()
    {
    //startourSnakeUpdateloop
    StartCoroutine(UpdateSnake());
    }

    //---------------------------------------------------------------------------------------------
    //UpdateSnake()
    //---------------------------------------------------------------------------------------------
    //Ourmainplayerloop, thisiswhatrunsthelogicforthesnakemovement, food, andaddingsegments
    //---------------------------------------------------------------------------------------------
    IEnumeratorUpdateSnake ()
    {
    while(true)
    {
    //handlemultikeypresses
    if (InputHelper.GetStandardMoveMultiInputKeys())
    {
    Debug.Log ("We are pressing multiple keys for direction");
    yieldreturnnull;
    continue;
    }

    //arewemovingup
    if (InputHelper.GetStandardMoveUpDirection())
    {
    yieldreturnStartCoroutine(MoveSnake(Direction.UP));
    //Debug.Log ("WearemovingUP");
    }
    //arewemovingleft
    if (InputHelper.GetStandardMoveLeftDirection())
    {
    yieldreturnStartCoroutine(MoveSnake(Direction.LEFT));
    //Debug.Log ("WearemovingLEFT");
    }

    //arewemovingdown
    if (InputHelper.GetStandardMoveDownDirection())
    {
    yieldreturnStartCoroutine(MoveSnake(Direction.DOWN));
    //Debug.Log ("WearemovingDOWN");
    }
    if (InputHelper.GetStandardMoveRightDirection())
    {
    yieldreturnStartCoroutine(MoveSnake(Direction.RIGHT));
    //Debug.Log ("WearemovingRIGHT");
    }

    //herewecheckforsnakecollision (itcanonlycollidewithitself)
    if (SnakeCollidedWithSelf() == true)
    {
    break; }
    yieldreturnnewWaitForSeconds(moveDelay);
    }

    //playourdeathsound
    audio.clip = death;
    audio.Play();

    //wearehit
    yieldreturnStartCoroutine(ScreenDeath.Instance.FlashDeathScreen());

    //wereducenumberoflives
    SnakeGame.Instance.UpdateLives(-1);

    //checkforplayablelivesleft
    if (SnakeGame.Instance.gameLives == 0)
    {
    //reloadthelevel, resettingthegame
    Application.LoadLevel("UniSnake");
    }
    else
    {
    //herewehandleresettingthesnakeafteracollision
    Initialize();

    //wehavetocallStartmanuallybecausethisobjectisalreadyInstantiated
    Start();
    }
    }

    //---------------------------------------------------------------------------------------------
    //MoveSnake()
    //---------------------------------------------------------------------------------------------
    //Movesthesnaketexture (pixelmovement / updatesnaketextureRect)
    //---------------------------------------------------------------------------------------------
    publicIEnumeratorMoveSnake(DirectionmoveDirection)
    {
    //defineatempListofRectstoourcurrentsnakesListofRects
    List<Rect> tempRects = newList<Rect>();
    RectsegmentRect = newRect(0,0,0,0);

    //initialize
    for (inti = 0; i < snakePos.Count; i++)
    {
    tempRects.Add(snakePos);
    }
    switch(moveDirection)
    {
    caseDirection.UP:
    if (snakePos[0].y > 94)
    {
    //wecanmoveup
    snakePos[0] = newRect(snakePos[0].x, snakePos[0].y - 20,
    snakePos[0].width, snakePos[0].height);
    //nowupdatetherestofourbody
    UpdateMovePosition(tempRects);

    //checkforfood
    if (CheckForFood() == true)
    {
    //checkforvalidbuildsegmentpositionandaddasegment

    //createatemporarycheckposition (thisoneisbelowthelastsegmentinsnakePos[])

    segmentRect = CheckForValidDownPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethodnotwantasegmentaboveus
    yieldbreak;
    }
    //createatemporarycheckposition (thisoneistotheleftthelastsegmentinsnakePos[])
    segmentRect = CheckForValidLeftPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }

    //createatemporarycheckposition (thisoneistotherightthelastsegmentinsnakePos[])
    segmentRect = CheckForValidRightPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
    }

    //noneedtocheckUp, becausewearepressingtheUpkey, wedonotwantasegmentaboveus
    }
    //toggletheaudioclipandplay
    audio.clip = (audio.clip == move1) ? move2 : move1;
    audio.Play();
    }
    break;
    caseDirection.LEFT:
    if (snakePos[0].x > 22)
    {
    //wecanmoveleft
    snakePos[0] = newRect(snakePos[0].x - 20, snakePos[0].y,
    snakePos[0].width, snakePos[0].height);

    //nowupdatetherestofourbody
    UpdateMovePosition(tempRects);

    //checkforfood
    if (CheckForFood() == true)
    {

    //checkforvalidbuildsegmentpositionandaddasegment
    //createatemporarycheckposition (thisoneistotherightthelastsegmentinsnakePos[])
    segmentRect = CheckForValidRightPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }

    //createatemporarycheckposition (thisoneisabovethelastsegmentinsnakePos[])
    segmentRect = CheckForValidUpPosition();
    if (segmentRect.x != 0)
    {

    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);


    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay

    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }

    //createatemporarycheckposition (thisoneisbelowthelastsegmentinsnakePos[])
    segmentRect = CheckForValidDownPosition();
    if (segmentRect.x != 0)
    {

    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f); //givecontrolbacktoourcallingmethod
    yieldbreak;
    }
    //noneedtocheckLeft, becausewearepressingtheLeftkey, wedonotwantasegmentaheadofus
    }
    //toggletheaudioclipandplay
    audio.clip = (audio.clip == move1) ? move2 : move1;
    audio.Play();
    }
    break;
    caseDirection.DOWN:
    if (snakePos[0].y < 654)
    {
    //wecanmovedown
    snakePos[0] = newRect(snakePos[0].x, snakePos[0].y + 20,
    snakePos[0].width, snakePos[0].height);

    //nowupdatetherestofourbody
    UpdateMovePosition(tempRects);

    //checkforfood
    if (CheckForFood() == true)
    {
    //checkforvalidbuildsegmentpositionandaddasegment
    //createatemporarycheckposition (thisoneisabovethelastsegmentinsnakePos[])

    segmentRect = CheckForValidUpPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;

    }

    //createatemporarycheckposition (thisoneistotheleftthelastsegmentinsnakePos[])
    segmentRect = CheckForValidLeftPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f); //givecontrolbacktoourcallingmethod
    yieldbreak;
    }
    //createatemporarycheckposition (thisoneistotherightthelastsegmentinsnakePos[])
    segmentRect = CheckForValidRightPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
    }

    //toggletheaudioclipandplay
    audio.clip = (audio.clip == move1) ? move2 : move1;
    audio.Play();

    }
    break;
    caseDirection.RIGHT:
    if (snakePos[0].x < 982)
    {
    //wecanmoveright
    snakePos[0] = newRect(snakePos[0].x + 20, snakePos[0].y, snakePos[0].width, snakePos[0].height);

    //nowupdatetherestofourbody
    UpdateMovePosition(tempRects);

    //checkforfood
    if (CheckForFood() == true)
    {
    //checkforvalidbuildsegmentpositionandaddasegment

    //createatemporarycheckposition (thisoneisleftofthelastsegmentinsnakePos[])
    segmentRect = CheckForValidLeftPosition();
    if (segmentRect.x != 0)
    {

    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }

    //createatemporarycheckposition (thisoneistotheleftthelastsegmentinsnakePos[])
    segmentRect = CheckForValidUpPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);

    //incrementoursnakelength
    snakeLength++;

    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

    //givecontrolbacktoourcallingmethod
    yieldbreak;
    }

    //createatemporarycheckposition (thisoneisbelowthelastsegmentinsnakePos[])
    segmentRect = CheckForValidDownPosition();
    if (segmentRect.x != 0)
    {
    //webuildanothersegmentpassingtheRectasanargument
    BuildSnakeSegment(segmentRect);
    //incrementoursnakelength
    snakeLength++;
    //decrementourmoveDelay
    moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
    }
    //noneedtocheckRight, becausewearepressingtheRightkey, wedonotwantasegmentaheadofus
    }

    //toggletheaudioclipandplay
    audio.clip = (audio.clip == move1) ? move2 : move1;
    audio.Play();
    }
    break;
    }

    yieldreturnnull;
    }
    //---------------------------------------------------------------------------------------------
    //UpdateMovePosition()
    //---------------------------------------------------------------------------------------------
    //UpdatesthesnakePoslistofRect'stothenewRectpositionsafteramove
    //---------------------------------------------------------------------------------------------
    privatevoidUpdateMovePosition(List<Rect> tmpRects)
    {

    {
    //updateoursnakePosRectwiththetmpRectpositions
    for (inti = 0; i < tmpRects.Count - 1; i++)
    //exe. sizeof3, assign1,2,3to0,1,2 - snakePos[0] isalreadyassigned
    snakePos[i+1] = tmpRects;
    }
    }
    //---------------------------------------------------------------------------------------------
    //CheckForFood()
    //---------------------------------------------------------------------------------------------
    //Checksifthefirstsnakesegmentissamepositionasthefoodonthegamefield, returnsbool
    //---------------------------------------------------------------------------------------------
    privateboolCheckForFood()
    {
    if(Food.Instance != null)
    {
    RectfoodRect = Food.Instance.foodPos;
    if (snakePos[0].Contains(newVector2(foodRect.x,foodRect.y)))
    {

    Debug.Log ("We hit the food");

    //were-positionthefood
    Food.Instance.UpdateFood();

    //weaddtoourscore
    SnakeGame.Instance.UpdateScore(1);
    returntrue;
    }
    }

    returnfalse;
    }

    //---------------------------------------------------------------------------------------------
    //CheckForValidDownPosition()
    //---------------------------------------------------------------------------------------------
    //Checksifthelastsnakesegmentisnotinthelowestpositiononthegamefield, returnsRect
    //---------------------------------------------------------------------------------------------
    privateRectCheckForValidDownPosition()
    {
    if (snakePos[snakePos.Count-1].y != 654)
    {
    returnnewRect(snakePos[snakePos.Count-1].x, snakePos[snakePos.Count-1].y - 20, 20, 20);
    }

    returnnewRect(0,0,0,0);
    }

    //---------------------------------------------------------------------------------------------
    //CheckForValidUpPosition()
    //---------------------------------------------------------------------------------------------
    //Checksifthelastsnakesegmentisnotinthehighestpositiononthegamefield, returnsRect
    //---------------------------------------------------------------------------------------------
    privateRectCheckForValidUpPosition()
    {
    if (snakePos[snakePos.Count-1].y != 94)
    {
    returnnewRect(snakePos[snakePos.Count-1].x, snakePos[snakePos.Count-1].y + 20, 20, 20);
    }
    returnnewRect(0,0,0,0);
    }

    //---------------------------------------------------------------------------------------------
    //CheckForValidLeftPosition()
    //---------------------------------------------------------------------------------------------
    //Checksifthelastsnakesegmentisnotinthefarleftpositiononthegamefield, returnsRect
    //---------------------------------------------------------------------------------------------
    privateRectCheckForValidLeftPosition()
    {
    if (snakePos[snakePos.Count-1].x != 22)
    {
    returnnewRect(snakePos[snakePos.Count-1].x - 20, snakePos[snakePos.Count-1].y, 20, 20);
    }
    returnnewRect(0,0,0,0);
    }

    //---------------------------------------------------------------------------------------------
    //CheckForValidRightPosition()
    //---------------------------------------------------------------------------------------------
    //Checksifthelastsnakesegmentisnotinthefarrightpositiononthegamefield, returnsRect
    //---------------------------------------------------------------------------------------------
    privateRectCheckForValidRightPosition()
    {
    if (snakePos[snakePos.Count-1].x != 982)
    {
    returnnewRect(snakePos[snakePos.Count-1].x + 20, snakePos[snakePos.Count-1].y, 20, 20);
    }

    returnnewRect(0,0,0,0);
    }

    //---------------------------------------------------------------------------------------------
    //BuildSnakeSegment()
    //---------------------------------------------------------------------------------------------
    //Addsasnakesegment, passtheRecttoapplythepositionto
    //---------------------------------------------------------------------------------------------
    privatevoidBuildSnakeSegment(RectrctPos)
    {
    //defineoursnakeheadandtailtexture
    snakeIcon.Add(TextureHelper.CreateTexture(20, 20, Color.green));

    //defineoursnakeheadandtailGUIRect
    snakePos.Add(rctPos);
    }

    //---------------------------------------------------------------------------------------------
    //SnakeCollidedWithSelf()
    //---------------------------------------------------------------------------------------------
    //Checksifthesnakehashitanypartofitsbody, returnstrue/false
    //---------------------------------------------------------------------------------------------
    privateboolSnakeCollidedWithSelf()
    {
    booldidCollide = false;

    if (snakePos.Count <= 4)
    {
    returnfalse;
    }
    for (inti = 0; i < snakePos.Count; i++)
    {
    if (i > 0)
    {
    if (snakePos[0].x == snakePos[snakePos.Count - i].x && snakePos[0].y ==
    snakePos[snakePos.Count - i].y)
    {
    //wehavecollided
    didCollide = true;
    break;
    }
    }
    }

    returndidCollide;
    }

    //---------------------------------------------------------------------------------------------
    //OnGUI()
    //---------------------------------------------------------------------------------------------
    //Unitymethod, handlesdisplayingthesnakeonscreen
    //---------------------------------------------------------------------------------------------
    voidOnGUI()
    {
    for (inti = 0; i < snakeLength; i++)
    {
    GUI.DrawTexture(snakePos, snakeIcon);
    }
    }

    //---------------------------------------------------------------------------------------------
    //Initialize()
    //---------------------------------------------------------------------------------------------
    //InitializesSnake
    //---------------------------------------------------------------------------------------------
    publicvoidInitialize()
    {
    print("Snake initialized");

    //clearourLists
    snakePos.Clear();
    snakeIcon.Clear();

    //initializeourlengthtostartlength
    snakeLength = 2;

    //intializeourmoveDelay
    moveDelay = 0.5f;

    //addourAudioSourcecomponent
    if (!gameObject.GetComponent<AudioSource>())
    {
    //loadinourclips
    move1 = Resources.Load("Sounds/Move1 Blip") asAudioClip;
    move2 = Resources.Load("Sounds/Move2 Blip") asAudioClip;
    death = Resources.Load("Sounds/Death") asAudioClip;
    gameObject.AddComponent<AudioSource>();

    //initializesomeaudioproperties
    audio.playOnAwake = false;
    audio.loop = false;
    audio.clip = move1;
    }

    //makesureourlocalScaleiscorrectforaGUItexture
    transform.position = Vector3.zero;
    transform.rotation = Quaternion.identity;
    transform.localScale = Vector3.one;

    //defineoursnakeheadandtailtexture
    snakeIcon.Add(TextureHelper.CreateTexture(20, 20, Color.green));
    snakeIcon.Add(TextureHelper.CreateTexture(20, 20, Color.green));

    //defineoursnakeheadandtailGUIRect
    snakePos.Add(newRect(Screen.width * 0.5f - 10, Screen.height * 0.5f - 10, snakeIcon[0].width,
    snakeIcon[0].height));
    snakePos.Add(newRect(Screen.width * 0.5f - 10 + 20, Screen.height * 0.5f - 10,
    snakeIcon[1].width, snakeIcon[1].height));
    }
    }