Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved error CS0106: The modifier 'private' is not valid for this item

Discussion in 'Scripting' started by Vitin_9336, May 28, 2020.

  1. Vitin_9336

    Vitin_9336

    Joined:
    May 27, 2020
    Posts:
    34
    Hello again, can you guys help me with this problem? I don't know how to resolve it, i think i'm dumb.
    Here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bullet : MonoBehaviour
    5. {
    6.     [Header("BulletConfig")]
    7.     public int range;
    8.     public int damage;
    9.     public float mass;
    10.     public float speed;
    11.     [Header("Imports")]
    12.     public PlayerController Player;
    13.     public ParticleSystem impact;
    14.  
    15.     //Privates
    16.     private Rigidbody rb;
    17.     private Vector3 origin;
    18.  
    19.     void Start()
    20.     {
    21.         rb = GetComponents<Rigidbody>();
    22.         origin = transform.position;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         //Massa
    28.         rb.mass = mass;
    29.  
    30.         //Frente
    31.         Vector3 horizontal = transform.right * 0;
    32.         Vector3 vertical = transform.forward * 1;
    33.         Vector3 velocity = (horizontal + vertical).normalized * speed;
    34.         rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
    35.  
    36.         //Range
    37.         if (Vector3.Distance(origin, transform.position) > range)
    38.         {
    39.             Destroy(GameObject);
    40.         }
    41.  
    42.         private void OnCollisionEnter(Collision collision)
    43.         {
    44.             Destroy(gameObject);
    45.             if (collision.gameObject.tag == "Terrain")
    46.             {
    47.                 GameObject eff = Instantiate(impact, collision.transform.position, Quaternion.Identity).gameObject;
    48.                 eff.transform.position += new Vector3(0, 0.1f, 0);
    49.                 eff.transform.eulerAngles = new Vector3(-90, 0, 0);
    50.             }
    51.         }
    52.     }
    53. }
     
  2. Vitin_9336

    Vitin_9336

    Joined:
    May 27, 2020
    Posts:
    34
    I'm really dumb :confused:
     
    Bunny83 likes this.
  3. The OnCollisionEnter method is inside the Update method. You need to rearrange your methods. Mainly the }-s.
     
    Bunny83 likes this.
  4. Vitin_9336

    Vitin_9336

    Joined:
    May 27, 2020
    Posts:
    34
    I need to put on Start method? Or on the "//Privates"?
     
  5. Just move the } in the 52th line to the 41st line.


    BTW, seeing your opened threads on the forums, you really need to learn how to format a basic C# script. Maybe this will help:
     
  6. Vitin_9336

    Vitin_9336

    Joined:
    May 27, 2020
    Posts:
    34
    Yes, i need help
     
  7. aabdalhakim6002

    aabdalhakim6002

    Joined:
    Jul 6, 2021
    Posts:
    1
    Assets\skript\Enemy.cs(72,9): error CS0106: The modifier 'private' is not valid for this item
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,053
    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  9. burgerandfries43

    burgerandfries43

    Joined:
    Jan 23, 2021
    Posts:
    8
    I just ran into the same problem i cannot believe how stupid i was thx for da fix
     
  10. timmyparker21

    timmyparker21

    Joined:
    Oct 15, 2022
    Posts:
    2
    Thank you this helped with my issues as well