Can't close start method , bug or wrong coding ?

Hey , i m a newcomer to Unity , had a little c sharp background . I’m trying to write a basic script but monobehaviour won’t let me close start() method .

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;


    // Start is called before the first frame update

    void Start() {
        public bool goLeft = Input.GetKey("a");
        public bool goRight = Input.GetKey("d");
}    


// Update is called once per frame
void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis

        if (moveRight)
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
        }

        if (moveLeft)
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
        }
    }
}

I’m not able to close the start method , simply program doesn’t let me , closes monobehaviour instead of the method and it gives an error , i just want to create my variables in there , am i doing a wrong coding or is this about softwares ?

Welcome! Be sure to use code tags when sharing code, as it formats the code more easily for us to read.

7355060--895388--upload_2021-7-23_8-3-52.png

public class PlayerMovement: MonoBehaviour {  // class Open

  public Rigidbody rb;

  public float forwardForce = 2000 f;
  public float sidewaysForce = 500 f;

  // Start is called before the first frame update

  void Start() {  // Start Open
    public bool goLeft = Input.GetKey("a");
    public bool goRight = Input.GetKey("d");
  }  // Start Closed

  // Update is called once per frame
  void FixedUpdate() {  // FixedUpdate Open
    rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis

    if (moveRight) {  // if Open
      rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    }  // if Closed

    if (moveLeft) {  // if Open
      rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    }  // if Closed
  }  // FixedUpdate Closed
}  // class Closed

That all looks fine to me. Is that what’s actually in your editor? Because if so, you’re good to go!

sry about formatting , yeah it is , i guess its about softwares then , my licenses are very old and shared (university) , also computers are very limited and i dont have all permissions , thanks for reply

My IDE (visual studio 19) doesn t let me close the start method , bracket closing the main mono behaviour method instead (only working normal if start method is empty)

What do you mean by “close”? Your code looks correct, you only need to indent the closing bracket to make it look better, but it’s in the correct location.

the moment i write something inside the start method , the bracket afterwards gone null , then it starts affecting the main method of mono behaviour , intellisense or unity doesn t matter , both gives error about missing bracket of start method , dunno its very weird

What do you mean “gone null”, you just need to type in the closing bracket like you have. The code you wrote is correct. Can you share a screenshot of the actual issue? The editor won’t always enter the bracket for you, so you just type it in.

7355723--895475--upload_2021-7-23_17-54-6.png

As mentioned, you just type in the closing bracket, your code that you posted is correct. Can you post the full script? You are not including any Using statements in the code that you posted.

uh , project gives errors and i can t find the script file right now , but it had unity lib - system - collections - generics

I typically create an Assets/Scripts folder in my projects, and keep my scripts there.

new to program , i am at tutorial part :sweat_smile: (oh found it)

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

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;


    // Start is called before the first frame update

    void Start() {
        public bool goLeft = Input.GetKey("a");
        public bool goRight = Input.GetKey("d");
}       


// Update is called once per frame
void FixedUpdate() 
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis

        if (moveRight)
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
        }

        if (moveLeft)
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
        }
    }
}

This code also looks correct.

@NeoBulbazour Your indenting is all janked as hell, but the curly braces do match up correctly. Is the code exactly like this giving you errors? If so, are you sure you have the file saved?

Yes i ’ m sure , it was working fine before this , i just tried to enhance the base code a little , i guess problem is about my IDE .

Thanks for the replies

You haven’t actually told us what IDE you’re using. Is it Visual Studio? VS Code? Notepad++?

I don’t think your bool variables on lines 12 and 13 in the Start method should be declared public?

I think you’re looking to do something like this:

declare the bools as class variables, ie put them at the top of the class so they can be used by other methods.
in Update() assign them according to the keypress
in FixedUpdate() test them

4 Likes