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

A very big problem: Expressions in statements must only be executed for their side-effects

Discussion in 'Scripting' started by Albertounitytutoriales, May 29, 2015.

  1. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    I have a very big problem. When I build my script, MonoDevelop say this:

    -Expressions in statements must only be executed for their side effects.This is my script:

    var walkAcceleration : float = 5;

    var cameraObject : GameObject;

    var maxWalkSpeed : float = 20;

    @HideInInspector

    var horizontalMovement : Vector2;

    function Update ()
    {
    horizontalMovement = Vector2(GetComponent.<Rigidbody>().velocity.x, GetComponent.<Rigidbody>().velocity.z);
    if(horizontalMovement.magnitude > maxWalkSpeed)
    {
    horizontalMovement.normalized;
    horizontalMovement *= maxWalkSpeed;
    }

    GetComponent.<Rigidbody>().velocity.x = horizontalMovement.x;
    GetComponent.<Rigidbody>().velocity.z = horizontalMovement.y;

    transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).RotacionActualY, 0);
    GetComponent.<Rigidbody>().AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
    }

    PD: I´m in Unity 5
    Please Help Me!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    [ code] [ /code] tags when you paste in code to the forums (sticky at the top of the section for that)

    errors usually have line numbers, which line is it complaining about?


    edit: could be this one?

    Code (csharp):
    1.  
    2. horizontalMovement.normalized;
    3.  
    http://docs.unity3d.com/ScriptReference/Vector2-normalized.html

    I think for that form you will need to do

    Code (csharp):
    1.  
    2. horizontalMovement = horizontalMovement.normalized;
    3.  
    or
    Code (csharp):
    1.  
    2. horizontalMovement.Normalize();
    3.  
    4.  
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    As LeftyRighty said, code tags please.

    I expect this line is your issue:

    Code (csharp):
    1. horizontalMovement.normalized;
    That code has no effect since it 'returns' a normalized version that you're not doing anything with.

    That should be:

    Code (csharp):
    1. horizontalMovement = horizontalMovement.normalized * maxWalkSpeed;
     
  4. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    The error is in the line 14
     
  5. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    In horizontalMovement.normalized;
     
  6. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    I SOLVE THE PROBLEM. THANKS EVERYBODY :)
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Please use [ code] tags and proper formatting in your original post and when ever you post code - it's very difficult to read without them.