Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Problem sorry i am a noob

Discussion in 'Getting Started' started by unity_7CEF556C5E1026FA35F6, Nov 21, 2023.

  1. unity_7CEF556C5E1026FA35F6

    unity_7CEF556C5E1026FA35F6

    Joined:
    Nov 21, 2023
    Posts:
    3
    The Console says "error CS0106: The modifier 'public' is not valid for this item"

    what can i do?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public Rigidbody2D rb2D;
    9.  
    10.     private bool grounded = true;
    11.  
    12.     public GameObject playercamera;
    13.  
    14.     bool rightButtonPressed = false;
    15.     bool leftButtonPressed = false;
    16.     bool jumpbutton = false;
    17.  
    18.     private void OnTriggerStay2D(Collider2D collision)
    19.     {
    20.         grounded = true;
    21.     }
    22.  
    23.     private void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         grounded = true;
    26.     }
    27.  
    28.     private void OnTriggerExit2D(Collider2D collision)
    29.     {
    30.         grounded = false;
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         ProcessInput();
    36.         CheckIfDead();
    37.         SmartphoneController();
    38.  
    39.     }
    40.  
    41.     void CheckIfDead()
    42.     {
    43.         if (transform.position.y < -12.1f)
    44.         {
    45.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    46.         }
    47.     }
    48.  
    49.     void ProcessInput()
    50.     {
    51.         rb2D.velocity = new Vector2(0, rb2D.velocity.y);
    52.  
    53.         if (Input.GetKey(KeyCode.D || rightButtonPressed))
    54.         {
    55.             rb2D.velocity = new Vector2(10, rb2D.velocity.y);
    56.         }
    57.  
    58.         if (Input.GetKey(KeyCode.A || leftButtonPressed))
    59.         {
    60.             rb2D.velocity = new Vector2(-10, rb2D.velocity.y);
    61.         }
    62.  
    63.         if (Input.GetKeyDown(KeyCode.Space) && grounded || jumpButton)
    64.         {
    65.             rb2D.AddForce(new Vector2(0, 1000));
    66.         }
    67.  
    68.     void SmartphoneController()    // here are the us isue
    69.         {
    70.  
    71.             public void SetrightButtonPressed(bool pressed)
    72.             {
    73.                 rightbuttonPressed = pressed;
    74.             }
    75.  
    76.             public void SetLeftButtonPressed(bool pressed)
    77.             {
    78.                 leftbuttonPressed = pressed;
    79.  
    80.             }
    81.  
    82.             public void jumpButtonPressed(bool pressed)
    83.             {
    84.                 jumpbutton = pressed;
    85.             }
    86.         }
    87.  
    88.    
    89.     }
    90.  
    91. }
    92.  
     
  2. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    249
    You cannot put a void method inside another void. Juste remove them from void SmartphoneController().

    You might need to teach yourself about C#.
     
    Bunny83 likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    763
    The brace that was on line 67 slipped and fell all the way down to line 89. Air lift it back up.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,165
    Actually, you can put another method into another method, but the method within can't be public. They are known as Local Functions.
    https://learn.microsoft.com/en-us/d...ing-guide/classes-and-structs/local-functions

    But yes, this isn't likely what OP was trying to do.
     
    Chubzdoomer, Bunny83 and halley like this.
  5. unity_7CEF556C5E1026FA35F6

    unity_7CEF556C5E1026FA35F6

    Joined:
    Nov 21, 2023
    Posts:
    3
    Thanks to all. Sorry, I'm a beginner and don't fully understand the connections yet
     
  6. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    249
  7. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    249