Search Unity

Got error CS0106 and I'm not sure why

Discussion in 'Scripting' started by wienerfrog, May 15, 2020.

  1. wienerfrog

    wienerfrog

    Joined:
    May 15, 2020
    Posts:
    3
    I'm a complete newbie to coding and am doing this for a school project so this is probably a completely obvious error on my part that I'm just not realizing but on line 45 I'm getting error CS0106 The modifier 'private' is not valid for this item
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.  
    9.     Rigidbody2D rb;
    10.  
    11.  
    12.     [SerializeField]
    13.     float jumpForce;
    14.  
    15.     bool grounded;
    16.  
    17.     private void Awake()
    18.     {
    19.         rb = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             if (grounded)
    30.             {
    31.  
    32.                 Jump();
    33.             }
    34.  
    35.         }
    36.  
    37.         void Jump()
    38.         {
    39.             grounded = false;
    40.  
    41.             rb.velocity = Vector2.up * jumpForce;
    42.         }
    43.  
    44.  
    45.         private void OnCollisionEnter2D(Collision2D collision)
    46.      
    47.         {
    48.             if (collision.gameObject.tag == "Ground")
    49.             {
    50.                 grounded = true;
    51.             }
    52.         }
    53.  
    54. }   }
    55.  
     
  2. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
    You cant call OnCollisionEnter2D from inside the update function. Idk whether this was intentional or whether you just haven't looked at where your brackets are, but at the moment you are declaring Jump and OnCollisionEnter as nested functions. Jump can be there as long as you don't want to use that code in other functions, but as OnCollisionEnter is a callback function, and built in to unity it has to be called outside the update function. if that makes sense.
     
  3. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.  
    11.     [SerializeField]
    12.     float jumpForce;
    13.  
    14.     bool grounded;
    15.  
    16.     private void Awake()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.  
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (Input.GetMouseButtonDown(0))
    27.         {
    28.             if (grounded)
    29.             {
    30.  
    31.                 Jump();
    32.             }
    33.  
    34.         }
    35.  
    36.         void Jump()
    37.         {
    38.             grounded = false;
    39.  
    40.             rb.velocity = Vector2.up * jumpForce;
    41.         }
    42.     }
    43.  
    44.     private void OnCollisionEnter2D(Collision2D collision)
    45.  
    46.     {
    47.         if (collision.gameObject.tag == "Ground")
    48.         {
    49.             grounded = true;
    50.         }
    51.     }
    52. }
    53.  
    This is what I imagine you want your code to look like...
     
  4. wienerfrog

    wienerfrog

    Joined:
    May 15, 2020
    Posts:
    3
    I know I'm late with this but thank you so much!