Search Unity

} Error

Discussion in 'Scripting' started by dadimartino, May 9, 2019.

  1. dadimartino

    dadimartino

    Joined:
    Mar 13, 2019
    Posts:
    9
    Hi Unity Community! I was using the Tanks Tutorial & C# Scripting. There is an error that pops up that says
    "error CS1513 : } expected."

    The Whole Script




    using UnityEngine;

    public class TankMovement : MonoBehaviour
    {
    public int m_PlayerNumber = 1;
    public float m_Speed = 12f;
    public float m_TurnSpeed = 180f;
    public AudioSource m_MovementAudio;
    public AudioClip m_EngineIdling;
    public AudioClip m_EngineDriving;
    public float m_PitchRange = 0.2f;


    private string m_MovementAxisName;
    private string m_TurnAxisName;
    private Rigidbody m_Rigidbody;
    private float m_MovementInputValue;
    private float m_TurnInputValue;
    private float m_OriginalPitch;


    private void Awake()
    {
    m_Rigidbody = GetComponent<Rigidbody>();
    }


    private void OnEnable ()
    {
    m_Rigidbody.isKinematic = false;
    m_MovementInputValue = 0f;
    m_TurnInputValue = 0f;
    }


    private void OnDisable ()
    {
    m_Rigidbody.isKinematic = true;
    }


    private void Start()
    {
    m_MovementAxisName = "Vertical" + m_PlayerNumber;
    m_TurnAxisName = "Horizontal" + m_PlayerNumber;

    m_OriginalPitch = m_MovementAudio.pitch;
    }


    private void Update()
    {
    // Store the player's input and make sure the audio for the engine is playing.
    m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
    m_TurnInputValue = Input.GetAxis(m_TurnAxisName);

    EngineAudio();
    }


    private void EngineAudio()
    {
    // Play the correct audio clip based on whether or not the tank is moving and what audio is currently playing.

    if (Mathf.Abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
    {
    if (m_MovementAudio.clip == m_EngineDriving);
    }
    {
    m_MovementAudio.clip = m_EngineIdling;
    m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    m_MovementAudio.Play();
    } <THIS IS THE BRACKET


    else
    {
    if (m_MovementAudio.clip == m_EngineIdling);
    }
    {
    m_MovementAudio.clip = m_EngineDriving;
    m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    m_MovementAudio.Play();
    }
    }
    }


    private void FixedUpdate()
    {
    // Move and turn the tank.
    Move();
    Turn();
    }


    private void Move()
    {
    // Adjust the position of the tank based on the player's input.
    Vector3 movement = transform.forward * m_MovementInputValue * m_speed * Time.deltaTime;

    m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
    }


    private void Turn()
    {
    // Adjust the rotation of the tank based on the player's input
    float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;

    Quaternion turnRotation = Quaternion.Euler(0.f, turn, 0f);

    m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
    }
     
    Last edited: May 9, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Please use code formatting. See the first post in this forum.

    I think your last three functions are not within the brackets of your class but it's hard to tell without proper formatting.
     
    dadimartino, Antypodish and Dextozz like this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Once script is formatted, it is easy to spot missing brackets.
     
    dadimartino likes this.
  4. dadimartino

    dadimartino

    Joined:
    Mar 13, 2019
    Posts:
    9
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your EngineAudio method has lots of problems. You have "if" statements with semicolons, and you have what appear to be reversed uses of curly braces. Use "{" to open and "}" to close, not the other way around.

    Also, post code using CODE tags so people can read it easier.
     
  6. dadimartino

    dadimartino

    Joined:
    Mar 13, 2019
    Posts:
    9

    I have the brackets the right way around and the video that told me to do this the "if" statements are all correct
    57:27
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    No. The syntax in the script is completely broken.

    Use a proper code editor, not notepad, and it will highlight the issues and help you correct them.
     
    Joe-Censored likes this.
  8. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    Your code:
    Code (CSharp):
    1. private void EngineAudio()
    2. {
    3.     if (Mathf.Abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
    4.     {
    5.         if (m_MovementAudio.clip == m_EngineDriving);
    6.             // Then what?
    7.     }
    8.  
    9.     // What's the point of this scope?
    10.     {
    11.         m_MovementAudio.clip = m_EngineIdling;
    12.         m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    13.         m_MovementAudio.Play();
    14.     }
    15.  
    16.     // An else that makes no sense
    17.     else
    18.     {
    19.         if (m_MovementAudio.clip == m_EngineIdling);
    20.             // Then what? A scope or command should follow a condition.
    21.     }
    22.    
    23.     // Another pointless scope
    24.     {
    25.         m_MovementAudio.clip = m_EngineDriving;
    26.         m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    27.         m_MovementAudio.Play();
    28.     }
    29.    
    30.     } // Orphaned curly brace
    31. }
    What I assume it should be:
    Code (CSharp):
    1. private void EngineAudio()
    2. {
    3.     if (Mathf.Abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
    4.     {
    5.         if (m_MovementAudio.clip == m_EngineDriving)
    6.         {
    7.             m_MovementAudio.clip = m_EngineIdling;
    8.             m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    9.             m_MovementAudio.Play();
    10.         }
    11.     }
    12.     else
    13.     {
    14.         if (m_MovementAudio.clip == m_EngineIdling)
    15.         {
    16.             m_MovementAudio.clip = m_EngineDriving;
    17.             m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
    18.             m_MovementAudio.Play();
    19.         }
    20.     }
    21. }
    Have a look to see the differences between the two to learn how you messed it up.