Search Unity

Compiling errors CS0116 Jumping Ball

Discussion in 'Getting Started' started by whistlinghook2000, May 17, 2020.

  1. whistlinghook2000

    whistlinghook2000

    Joined:
    May 17, 2020
    Posts:
    7
    I tried making the ball jump although when I enter unity I have 3 errors but i have no idea why. I'm very new to this and I'm trying to add onto the unity roll a ball tutorial.
    The compiling errors are:

    1. Assets/scripts/PlayerController.cs(74,6); error CS0116: A namespace cannot directly contain members such as fields or methods
    2. Assets/scripts/PlayerController.cs(64,6; error CS0116: A namespace cannot directly contain members such as fields or methods

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public float JumpPower = 1f;
    10.     public Text countText;
    11.     public Text winText;
    12.  
    13.     private Vector3 moveDirection;
    14.     private Rigidbody rb;
    15.     private int count;
    16.     private bool jump;
    17.     private bool isGrounded;
    18.     private Vector3 jumpDirection;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.         count = 0;
    24.         SetCountText();
    25.         winText.text = "";
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         float moveHorizontal = Input.GetAxis("Horizontal");
    31.         float moveVertical = Input.GetAxis("Vertical");
    32.  
    33.         jump = Input.GetButton("Jump");
    34.  
    35.         moveDirection = new Vector3(moveHorizontal, 0.0f, moveVertical);
    36.     }
    37.  
    38.     void FixedUpdate()
    39.     {
    40.         Move();
    41.         Jump();
    42.     }
    43.     void Move()
    44.     {
    45.         rb.AddForce(movement * speed);
    46.     }
    47.     void Jump()
    48.     {
    49.         LayerMask layer = 1 << gameObject.layer;
    50.         layer = ~layer;
    51.         isGrounded = Physics.CheckSphere(transform.position, 1f, layer);
    52.  
    53.         if (jump && isGrounded)
    54.         {
    55.             rb.AddForce(jumpDirection * jumpPower, ForceMode.Impulse);
    56.         }
    57.     }
    58.     void OnCollisionEnter(Collision collision)
    59.     {
    60.         jumpDirection = collision.contacts[0].normal;
    61.     }
    62. }
    63.  
    64.  
    65. void OnTriggerEnter(Collider other)
    66. {
    67.     if (other.gameObject.CompareTag("Pick Up"))
    68.     {
    69.         other.gameObject.SetActive(false);
    70.         count = count + 1;
    71.         SetCountText();
    72.     }
    73. }
    74.  
    75. void SetCountText()
    76. {
    77.     countText.text = "Count: " + count.ToString();
    78.     if (count >= 12)
    79.     {
    80.         winText.text = "You Win!";
    81.     }
    82. }
    83.  
    84.  
    85.  
     

    Attached Files:

    Last edited: May 17, 2020
  2. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    The closing brace on your line 62 puts your functions 'void OnTriggerEnter' and 'void SetCountText' outside your class body. Move this closing brace to your line 83.
     
    whistlinghook2000 likes this.
  3. whistlinghook2000

    whistlinghook2000

    Joined:
    May 17, 2020
    Posts:
    7
    Thank you