Search Unity

error CS0844 and error CS0034

Discussion in '2D' started by Twitch_ByTaXx, Dec 4, 2020.

Thread Status:
Not open for further replies.
  1. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    Hello unity community !! I'm starting with this 3d programming, and even following the tutorials I get the errors that I mentioned in the title. Could someone help me? I leave the script here:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player : MonoBehaviour {
    6.  
    7.     public float speed = 4f;
    8.  
    9.     Animator anim;
    10.     Rigidbody2D rb2d;
    11.     Vector2 mov;
    12.  
    13.     void Start () {
    14.         anim = GetComponent<Animator>();
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     void Update () {
    19.  
    20.         mov = new Vector2(
    21.             Input.GetAxisRaw("Horizontal"),
    22.             Input.GetAxisRaw("Vertical")
    23.         );
    24.  
    25.         Vector3 mov = new Vector3(
    26.             Input.GetAxisRaw("Horizontal"),
    27.             Input.GetAxisRaw("Vertical"),
    28.             0
    29.         );
    30.  
    31.         transform.position = Vector3.MoveTowards(
    32.             transform.position,
    33.             transform.position + mov,
    34.             speed * Time.deltaTime
    35.         );
    36.  
    37.         if (mov != Vector2.zero) {
    38.             anim.SetFloat("movX", mov.x);
    39.             anim.SetFloat("movY", mov.y);
    40.             anim.SetBool("walking", true);
    41.         } else {  
    42.             anim.SetBool("walking", false);
    43.         }
    44.  
    45.     }
    46.  
    47.     void FixedUpdate () {
    48.         rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
    49.        
    50.     }
    51.  
    52. }  

    this is what it puts in the errors:

    20,9): error CS0844: Cannot use local variable 'mov' before it is declared. The declaration of the local variable hides the field 'player.mov'.

    37,13): error CS0034: Operator '!=' is ambiguous on operands of type 'Vector3' and 'Vector2'
     
  2. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    Sorry is 2d programme xd
     
  3. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    vector3 and vector 2 is different

    you are using the same name "mov" for different variables
     
  4. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    ok but, how do i solve it? I am new here :)
     
  5. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class player : MonoBehaviour {
    6.    
    7.         public float speed = 4f;
    8.    
    9.         Animator anim;
    10.         Rigidbody2D rb2d;
    11.         Vector2 mov;
    12.    
    13.         void Start () {
    14.             anim = GetComponent<Animator>();
    15.             rb2d = GetComponent<Rigidbody2D>();
    16.         }
    17.    
    18.         void Update () {
    19.    
    20.             mov = new Vector2(
    21.                 Input.GetAxisRaw("Horizontal"),
    22.                 Input.GetAxisRaw("Vertical")
    23.             );
    24.    
    25.             Vector3 mv = new Vector3(
    26.                 Input.GetAxisRaw("Horizontal"),
    27.                 Input.GetAxisRaw("Vertical"),
    28.                 0
    29.             );
    30.    
    31.             transform.position = Vector3.MoveTowards(
    32.                 transform.position,
    33.                 transform.position + mv,
    34.                 speed * Time.deltaTime
    35.             );
    36.    
    37.             if (mov != Vector2.zero) {
    38.                 anim.SetFloat("movX", mov.x);
    39.                 anim.SetFloat("movY", mov.y);
    40.                 anim.SetBool("walking", true);
    41.             } else {
    42.                 anim.SetBool("walking", false);
    43.             }
    44.    
    45.         }
    46.    
    47.         void FixedUpdate () {
    48.             rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
    49.          
    50.         }
    51.    
    52.     }
    53.  
     
  6. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    [QUOTE = "raarc, publicación: 6591676, miembro: 5322638"]
    Muchas gracias, no estoy en casa pero cuando llegue lo intentaré. Y sabes también cómo solucionar el otro error?
     
  7. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    both errors are from the same reason
     
  8. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    [QUOTE = "raarc, publicación: 6591967, miembro: 5322638"] ambos errores se deben al mismo motivo [/ QUOTE]
    Ok Bro muchas gracias por tu ayudaaa
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Twitch_ByTaXx

    Use English...?

    Usually it is best to first see if you have red (or whatever color) squiggly lines under your code in your IDE. Those lines are usually the source of your problem.

    You declare two different mov variables.

    One is a class level field (of type Vector2). Other is declared in your Update (of type Vector3).

    And then you try to do
    mov != Vector2.zero
    .

    So you try to compare your Update's mov (which is Vector3) to Vector2 - so you'll get an error.

    Also, this is a general scripting question. So you are asking this in a wrong forum.
     
    Last edited: Dec 5, 2020
  10. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    [QUOTE = "eses, publicación: 6592060, miembro: 206792"] [USER = 6341869] [USER = 6341869]

    yo hablo español pero con el traductor me podrás entender xd. Con hacer lo que me puso el usuario anterior que me comentlo podré arreglar? [/ ICODE]
     
  11. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Use English.

    And please try to use tags properly.
     
    Twitch_ByTaXx likes this.
  12. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    Thank you very much, if it worked !!
     
  13. Twitch_ByTaXx

    Twitch_ByTaXx

    Joined:
    Dec 4, 2020
    Posts:
    11
    Thank you very much, if it worked !!
     
  14. sbogdannicolae

    sbogdannicolae

    Joined:
    Dec 23, 2021
    Posts:
    2
    Im trying to make my character to move up when i press Z but I get this error
    CS0034 Operator '+=' is ambiguous on operands of type 'Vector 3' and 'Vector 2'



    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MovementScript : MonoBehaviour
    4. {
    5.     float speed = 20;
    6.     Vector2 Vector2 = new Vector2(0, 3);
    7.  
    8.  
    9.  
    10.     public void Update()
    11.     {
    12.         if (Input.GetKey(KeyCode.Z))
    13.         {
    14.             transform.position += Vector2 * speed * UnityEngine.Time.deltaTime;
    15.         }
    16.     }
    17. }
    18.  
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Please don't necro-post to threads from 2020.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    To solve your problem you might change your design use a Vector3 in lieu of Vector2

    Alternately any other way of disambiguating what you intend by the vector addition will also suffice.

    Also, don't name your variable (Vector2) the same as the type. It is totally legal, but oh my goodness, it's 2022, be kind to yourself!
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    As above but also, please post scripting issues on the scripting forum.
     
Thread Status:
Not open for further replies.