Search Unity

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

Discussion in 'Getting Started' started by NeoBulbazour, Jul 23, 2021.

  1. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    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 .

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour {
    2.  
    3.     public Rigidbody rb;
    4.  
    5.     public float forwardForce = 2000f;
    6.     public float sidewaysForce = 500f;
    7.  
    8.  
    9.     // Start is called before the first frame update
    10.  
    11.     void Start() {
    12.         public bool goLeft = Input.GetKey("a");
    13.         public bool goRight = Input.GetKey("d");
    14. }    
    15.  
    16.  
    17. // Update is called once per frame
    18. void FixedUpdate()
    19.     {
    20.         rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis
    21.  
    22.         if (moveRight)
    23.         {
    24.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    25.         }
    26.  
    27.         if (moveLeft)
    28.         {
    29.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    30.         }
    31.     }
    32. }


    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 ?
     
    Last edited: Jul 23, 2021
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome! Be sure to use code tags when sharing code, as it formats the code more easily for us to read.

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

    Code (CSharp):
    1. public class PlayerMovement: MonoBehaviour {  // class Open
    2.  
    3.   public Rigidbody rb;
    4.  
    5.   public float forwardForce = 2000 f;
    6.   public float sidewaysForce = 500 f;
    7.  
    8.   // Start is called before the first frame update
    9.  
    10.   void Start() {  // Start Open
    11.     public bool goLeft = Input.GetKey("a");
    12.     public bool goRight = Input.GetKey("d");
    13.   }  // Start Closed
    14.  
    15.   // Update is called once per frame
    16.   void FixedUpdate() {  // FixedUpdate Open
    17.     rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis
    18.  
    19.     if (moveRight) {  // if Open
    20.       rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    21.     }  // if Closed
    22.  
    23.     if (moveLeft) {  // if Open
    24.       rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    25.     }  // if Closed
    26.   }  // FixedUpdate Closed
    27. }  // class Closed
    That all looks fine to me. Is that what's actually in your editor? Because if so, you're good to go!
     
  3. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    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
     
  4. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    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)
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  6. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    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
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  8. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    upload_2021-7-23_17-54-6.png
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
    Last edited: Jul 23, 2021
  10. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    uh , project gives errors and i can t find the script file right now , but it had unity lib - system - collections - generics
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I typically create an Assets/Scripts folder in my projects, and keep my scripts there.
     
  12. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    new to program , i am at tutorial part :oops: (oh found it)
     
  13. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour {
    7.  
    8.     public Rigidbody rb;
    9.  
    10.     public float forwardForce = 2000f;
    11.     public float sidewaysForce = 500f;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.  
    16.     void Start() {
    17.         public bool goLeft = Input.GetKey("a");
    18.         public bool goRight = Input.GetKey("d");
    19. }      
    20.  
    21.  
    22. // Update is called once per frame
    23. void FixedUpdate()
    24.     {
    25.         rb.AddForce(0, 0, forwardForce * Time.deltaTime); // z-axis
    26.  
    27.         if (moveRight)
    28.         {
    29.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    30.         }
    31.  
    32.         if (moveLeft)
    33.         {
    34.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    35.         }
    36.     }
    37. }    
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    This code also looks correct.
     
  15. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @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?
     
  16. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    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 .
     
    Last edited: Jul 23, 2021
  17. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
    Thanks for the replies
     
  18. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You haven't actually told us what IDE you're using. Is it Visual Studio? VS Code? Notepad++?
     
  19. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15
     
  20. max3210

    max3210

    Joined:
    Apr 19, 2020
    Posts:
    17
    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
     
    Last edited: Jul 24, 2021
  21. NeoBulbazour

    NeoBulbazour

    Joined:
    Jul 23, 2021
    Posts:
    15

    worked out for me as well , thank you