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

can someone tell me what im doing wrong?

Discussion in 'Scripting' started by pubalreadyexists, Aug 21, 2022.

  1. pubalreadyexists

    pubalreadyexists

    Joined:
    Jan 31, 2022
    Posts:
    3
    unity is expecting a } but if I do that that one } will be too much and it won't work at all
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Character2DController : MonoBehaviour
    5. {
    6.     // Start is called before the first frame update
    7.     public float MovementSpeed = 1;
    8.     public float JumpForce = 1;
    9.  
    10.     private Rigidbody2D _rigidbody;
    11.  
    12.     private Animator anim;
    13.  
    14.     private void Start()
    15.     {
    16.         anim = GetComponent<Animator>();
    17.         _rigidbody = GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     private void Update()
    22.     {
    23.         var PlayerMovement = Input.GetAxis("Horizontal");
    24.         transform.position += new Vector3(PlayerMovement, 0, 0) * Time.deltaTime * MovementSpeed;
    25.  
    26.     if(PlayerMovement == 0)
    27.     {
    28.         anim.SetBool("isRunning", false);
    29.     }
    30.     else
    31.     {
    32.         anim.SetBool("isRunning", true);
    33.     }
    34.  
    35.     if(Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f) true;
    36.     {
    37.         _rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
    38.          anim.SetBool("isJumping", true);
    39.     }
    40.  
    41.          else if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) > 0.001f) false;
    42.          {
    43.             anim.SetBool("isJumping", false);
    44.          }
    45.        
    46.         }
    47.     }
    48.  
    49.  
     
    Last edited: Aug 21, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Please post code in code tags, not screenshots.

    Neither of your if and else if statements on line 34 and 39 are formatted correctly. Look at how your if statement on line 25 is formatted and see where the other two go wrong.
     
  3. pubalreadyexists

    pubalreadyexists

    Joined:
    Jan 31, 2022
    Posts:
    3
    I'm sorry for not putting it in code tags because I didn't know how to do so since I am a complete beginner and could you explain to me what you said in easier terms? (sorry if I have bad English)
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Look at your first if-else statement:
    Code (CSharp):
    1.     if (PlayerMovement == 0)
    2.     {
    3.         anim.SetBool("isRunning", false);
    4.     }
    5.     else
    6.     {
    7.         anim.SetBool("isRunning", true);
    8.     }
    Notice how
    PlayerMovement == 0
    is between two brackets?

    Now look at the next one:
    Code (CSharp):
    1. if(Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f) true;
    Why is there a
    true
    just hanging off the end?

    It should be:
    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f);
    It can also be easier to break these down into boolean values:
    Code (CSharp):
    1. bool jumpPressed = Input.GetButtonDown("Jump");
    2. bool notJumping = Mathf.Abs(_rigidbody.velocity.y) < 0.001f;
    3.  
    4. if (jumpPressed && notJumping)
    5. {
    6.  
    7. }
     
  5. pubalreadyexists

    pubalreadyexists

    Joined:
    Jan 31, 2022
    Posts:
    3
    I don't understand what you meant there at the end. I don't have a bool that signifies if my character is not jumping only that it is jumping so even If I were to bool the isJumping how would I use it so that the missing } will go away and how will I use it in the code and I don't know how the
    if (jumpPressed && notJumping)
    {

    }
    works
    and sorry if I am bothering you, I will go to sleep I will check my replies tomorrow so sorry if I have late replies
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    The first part of my post is separate to the last part.

    I showed you how to correct your formatting error. If that's all you care about just do that and move on.

    The last part was just showing how to make your code more readable. I know you don't have those booleans, it's just an example.

    All if-statements are doing is checking a boolean value, ergo, whether something true or false.

    For example:
    Code (CSharp):
    1. int someInt = 10;
    2. int anotherInt = 9;
    3.  
    4. if (someInt > anotherInt)
    5.  
    6. //the above gets reduced down to:
    7. if (true)
    And you can use boolen expressions to assign a boolean value:
    Code (CSharp):
    1. int someInt = 10;
    2. int anotherInt = 9;
    3.  
    4. bool someIntGreater = someInt > anotherInt; //expression equals true
    5.  
    6. if (someIntGreater) //boolean value is true so code will execute
    Can't really break it down any more than that. If this is confusing then more C# tutorials are in order for you.