Search Unity

The name Flip and Horizontal does not exsist in the current context. pls help

Discussion in 'Getting Started' started by JustJulius, Jul 4, 2019.

  1. JustJulius

    JustJulius

    Joined:
    Jul 2, 2019
    Posts:
    1
    How can I fix that they are existing?



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour {

    public float WallDownForce;
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    private bool facingRight;

    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround;

    private bool onWall;
    public Transform wallCheck;
    public float WallCheckRadius;
    public LayerMask whatIsWall;

    private int extraJumps;
    public int extraJumpsValue;

    // Start is called before the first frame update
    void Start() {
    extraJumps = extraJumpsValue;
    rb = GetComponent<Rigidbody2D>();
    facingRight = true;
    }

    void FixedUpdate() {

    Flip(Horizontal);

    onWall = Physics2D.OverlapCircle(wallCheck.position, WallCheckRadius, whatIsWall);
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }
    void Update(){

    if(isGrounded == true){
    extraJumps = 1;
    }
    if(onWall == true){
    extraJumps = 1;
    }
    if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0){
    rb.velocity = Vector2.up * jumpForce;
    extraJumps--;
    }else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true){
    rb.velocity = Vector2.up * jumpForce;

    if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0){
    rb.velocity = Vector2.up * jumpForce;
    extraJumps--;
    }else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && onWall == true){
    rb.velocity = Vector2.down * WallDownForce;
    }
    }
    void Flip(float Horizontal)
    {

    if (Horizontal > 0 && !facingRight || Horizontal < 0 && facingRight)
    {
    facingRight = !facingRight;

    Vector3 theScale = transform.localScale;

    theScale.x *= -1;

    transform.localScale = theScale;
    }

    }
    }
    }
     
  2. JoeStrout likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Also, copy & paste the actual error, including line number.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,144
    Check your parenthesis. This is almost always caused by having too many and in the wrong place.
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I believe it's this line, Horizontal is not defined

    Flip(Horizontal);

    Try Flip(0);

    and go from there. But please use code tags, I'm not sure but I believe Flip is improperly defined inside another method.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,144
    Yes. He's defined it inside of the Update() method. Below is the code with the function moved outside of it.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float WallDownForce;
    9.     public float speed;
    10.     public float jumpForce;
    11.     private float moveInput;
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.     private bool facingRight;
    16.  
    17.     private bool isGrounded;
    18.     public Transform groundCheck;
    19.     public float checkRadius;
    20.     public LayerMask whatIsGround;
    21.  
    22.     private bool onWall;
    23.     public Transform wallCheck;
    24.     public float WallCheckRadius;
    25.     public LayerMask whatIsWall;
    26.  
    27.     private int extraJumps;
    28.     public int extraJumpsValue;
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.         extraJumps = extraJumpsValue;
    34.         rb = GetComponent<Rigidbody2D>();
    35.         facingRight = true;
    36.     }
    37.  
    38.     void FixedUpdate()
    39.     {
    40.  
    41.         Flip(Horizontal);
    42.  
    43.         onWall = Physics2D.OverlapCircle(wallCheck.position, WallCheckRadius, whatIsWall);
    44.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    45.         moveInput = Input.GetAxis("Horizontal");
    46.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    47.     }
    48.     void Update()
    49.     {
    50.  
    51.         if (isGrounded == true)
    52.         {
    53.             extraJumps = 1;
    54.         }
    55.         if (onWall == true)
    56.         {
    57.             extraJumps = 1;
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    60.         {
    61.             rb.velocity = Vector2.up * jumpForce;
    62.             extraJumps--;
    63.         }
    64.         else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
    65.         {
    66.             rb.velocity = Vector2.up * jumpForce;
    67.  
    68.             if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    69.             {
    70.                 rb.velocity = Vector2.up * jumpForce;
    71.                 extraJumps--;
    72.             }
    73.             else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && onWall == true)
    74.             {
    75.                 rb.velocity = Vector2.down * WallDownForce;
    76.             }
    77.         }
    78.     }
    79.     void Flip(float Horizontal)
    80.     {
    81.  
    82.         if (Horizontal > 0 && !facingRight || Horizontal < 0 && facingRight)
    83.         {
    84.             facingRight = !facingRight;
    85.  
    86.             Vector3 theScale = transform.localScale;
    87.  
    88.             theScale.x *= -1;
    89.  
    90.             transform.localScale = theScale;
    91.         }
    92.  
    93.     }
    94. }