playerscript.cs error

hello everyone, I am a newbie to unity but i have been following a tutorial on pixelnest.io on how to get started making 2d games and so far everything has been going okay until i got to a part in the tutorial where i had to add on to my PlayerScript.cs. Once i added on to the PlayerScript after a certain point (update()) I am now getting the error: keyword void cannot be used in this context. I am looking for someone to help me out with this and maybe fix the error. Here is the full code for reference:

using UnityEngine;

/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
    /// <summary>
    /// 1 - The speed of the ship
    /// </summary>
    public Vector2 speed = new Vector2(50, 50);

    // 2 - Store the movement and the component
    private Vector2 movement;
    private Rigidbody2D rigidbodyComponent;

    void Update()
  {
        // 3 - Retrieve axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Movement per direction
        movement = new Vector2(
          speed.x * inputX,
          speed.y * inputY);
		  
  {
    // ...

    // 5 - Shooting
    bool shoot = Input.GetButtonDown("Fire1");
    shoot |= Input.GetButtonDown("Fire2");
    // Careful: For Mac users, ctrl + arrow is a bad idea

    if (shoot)
    {
      WeaponScript weapon = GetComponent<WeaponScript>();
      if (weapon != null)
      {
        // false because the player is not an enemy
        weapon.Attack(false);
      }
    }

    // ...
  }
	 {
    // ...

    // 6 - Make sure we are not outside the camera bounds
    var dist = (transform.position - Camera.main.transform.position).z;

    var leftBorder = Camera.main.ViewportToWorldPoint(
      new Vector3(0, 0, dist)
    ).x;

    var rightBorder = Camera.main.ViewportToWorldPoint(
      new Vector3(1, 0, dist)
    ).x;

    var topBorder = Camera.main.ViewportToWorldPoint(
      new Vector3(0, 0, dist)
    ).y;

    var bottomBorder = Camera.main.ViewportToWorldPoint(
      new Vector3(0, 1, dist)
    ).y;

    transform.position = new Vector3(
      Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
      Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
      transform.position.z
    );

    // End of the update method
  }
  
     void FixedUpdate()
    {
        // 5 - Get the component and store the reference
        if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();

        // 6 - Move the game object
        rigidbodyComponent.velocity = movement;
    }
}

So you have a nesting issue. You have a method inside of another method which you can not do. I’m going to strip out some code from what you have posted and show you what you’re doing, and how it should be.

What you’re doing:

void Update()
{
    void FixedUpdate()
    {
    }
}

See how you have a method inside of another method? That is not ok. So void is a keyword that exists as a non-return type method. That must be under a class, so you’re getting the void error message because void can’t be there. That’s what the compiler is throwing. So to fix the issues all you have to do is move the FixedUpdate method outside of the Update method.

void Update()
{}

void FixedUpdate()
{}

Then make sure that all your brackets have openings and endings for the method as well as the class and you should be right as rain. :slight_smile:

like this?

   {
		   void FixedUpdate()
		   {
			   if(rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
			   
			   rigidbodyComponent.velocity = movement;
			   
		   }
		   
	   }

like this?

   {
		   void FixedUpdate()
		   {
			   if(rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
			   
			   rigidbodyComponent.velocity = movement;
			   
		   }
		   
	   }