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. Dismiss Notice

cs errors

Discussion in 'Scripting' started by Mark_Wilson, Aug 18, 2014.

  1. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Hey guys I'm getting the following errors - cs0201:eek:nly assignment, call, increment, decrement, and new object expressions can be used as a statement.
    and cs1023:an embedded statement may not be a declaration or a labelled statement.
    Here's my code -
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class elephantMove : MonoBehaviour {
    5.  
    6.     public playerMove playerRight; // connects to the playerMove script, type playerRight to acess the script.
    7.    
    8.     Vector2 offset;
    9.     Vector2 elepos;
    10.  
    11.     Animator anim; //animator feature is now called anim
    12.    
    13.     // Use this for initialization
    14.     void Start () {
    15.         anim= GetComponent<Animator>(); //anim is linked to animator
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.    
    21.         offset = new Vector2(1, 0);
    22.         elepos = new Vector2(transform.position.x, transform.position.y);
    23.  
    24.         if(GetComponent<HingeJoint2D>().enabled == true){ // is the elephant connected to the player?
    25.  
    26.                 if(playerRight.facingRight == false)
    27.                  elepos ++ offset;
    28.                 transform.position = new Vector2(elepos.x,elepos.y);
    29.                
    30.                
    31.             if(playerRight.move < -0.3f && playerRight.facingRight == false) // if move is more than 0 and the player is NOT facing right
    32.                 anim.SetBool ("eleFlipped", true);
    33.                
    34.             else if(playerRight.move > 0.3f && playerRight.facingRight == true) //if move is less than 0 and facing right
    35.                 anim.SetBool ("eleFlipped", false);
    36.  
    37.             if (playerRight.playerMovingBool == true) // the player is moving
    38.                 anim.SetBool ("eleSpeed", true); //set the eleSpeed bool in the animator to true, this will call the walk animation
    39.  
    40.             if (playerRight.playerMovingBool == false) //the player is no longer moving
    41.                 anim.SetBool ("eleSpeed", false); //set the eleSpeed bool in the animator to false, this will call the idle animation
    42.         }
    43.  
    44.     }  
    45. }
    46.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Replace: elepos ++ offset;
    It should probably be elepos += offset;
     
  3. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Sorry, I didn't explain my problem. I want it to increment by the value of offset only once. += keeps on incrementing over and over.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Once per....? Once per second? Only once, period? Once each time some condition is met?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    So? You're initializing elepos on line 22. Then on line 27 you may add offset to it (just once). Then, the next time Update is called, you initialize it again.

    So elepos will always be either transform.position, or one unit to the right, unless you have some other code that's changing it elsewhere.
     
  6. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Just once. so the position of the sprite offsets when the if statement becomes true.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    So you need a boolean flag. Declare it outside the function, so that it is persistent. Check whether that is true in the if statement; inside the if statement, set it to false.
     
  8. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Ah okay. i'll give that a go :) the problem does seem to be due to the fact that the elepos++offset is inside that if statement, so hopefully it'll work.
    Cheers