-3

I am having trouble correcting an error on the final line of my code, where '}' comes up as an error ('} expected'). I am brand new at this but have hunted through my code and can't find the issue.

I have tried removing ; from the previous block and placing } closer and further away from the beginning of the line.

{ 
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();
        }
    }
    else
    {
        if (Mathf.abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
        {
            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();
            }
        }
    }


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


    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);
    }


    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(0f, turn, 0f);

        m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
    }

}
sticky bit
  • 36,626
  • 12
  • 31
  • 42
Tori Rock
  • 9
  • 1

2 Answers2

1

You're missing a closing } at the bottom of function 'EngineAudio' Since you are likely using Visual Studio, here's a tip for finding missing curly braces: put the cursor on an open or close brace, hold down the control key and then press the ] key: CTRL + ]. This will move the cursor to the matching open or close brace. If the cursor doesn't move, then you have a problem inside that block. You can test all the braces in your code section this way and see where the missing one should be. Also works with round braces (a.k.a parentheses). Check this out: Where's the closing brace?

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

    if (Math.Abs(m_MovementInputValue) < 0.1f && Math.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();
        }
    }
    else
    {
        if (Math.abs(m_MovementInputValue) < 0.1f && Math.Abs(m_TurnInputValue) < 0.1f)
        {
            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();
            }
        }
    }
} //<---add this one right here.

void FixedUpdate()
{
     // Move and turn the tank.
     Move();
     Turn();
}
Display name
  • 1,228
  • 1
  • 18
  • 29
  • If it's just a typo, why not just comment and flag to close? This kind of question is unlikely to be useful to future readers. – EJoshuaS - Stand with Ukraine Apr 17 '19 at 20:26
  • It's not just a type-o. It's a missing closing brace. For novice coders, these can be difficult to spot. And I also pointed out the easy way to find these in the future. Find a coder a brace and he compiles for the day. Show him how to find them himself he codes for a lifetime. :) – Display name Apr 17 '19 at 20:30
  • The Visual Studio hint is admittedly a useful addition. I guess I'll go with +1 for that being a useful thing to do to identify this kind of problem. – EJoshuaS - Stand with Ukraine Apr 17 '19 at 20:32
0

I see there's a } outside of the codeblock you have in your question, assuming that goes to your private void EngineAudio(), it looks like you have an opening bracket before your variables at the beginning with no closing bracket after the one I just mentioned.

So possibly:

{ 
  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();
          }
      }
      else
      {
          if (Mathf.abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
          {
              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();
              }
          }
      }


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


      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);
      }


      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(0f, turn, 0f);

          m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
      }
  } // void EngineAudio()
} // missing closing bracket ? 
Kam
  • 116
  • 1
  • 7