Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

The modifier 'private' is not valid for this item

Discussion in '2D' started by LukasAlstrup, Mar 22, 2020.

Thread Status:
Not open for further replies.
  1. LukasAlstrup

    LukasAlstrup

    Joined:
    Mar 18, 2020
    Posts:
    3
    Hello!
    I followed a YouTube tutorial on how to make a 2D game in Unity by Kyle Suchar.


    But when I run the game it says:
    Assets\Grounded.cs(40,5): error CS0106: The modifier 'private' is not valid for this item
    All compiler errors have to be fixed before you can enter playmode!
    UnityEditor.SceneView:ShowCompileErrorNotification()

    My c#:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Grounded : MonoBehaviour
    {
    GameObject Player;
    // Start is called before the first frame update
    void Start()
    {
    Player = gameObject.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update()
    {

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.collider.tag == "Ground")
    {
    Player.GetComponent<Move2D>().isGrounded = true;
    }
    }





    private void OnCollisionExit2D(Collision2D collision)
    {
    if (collision.collider.tag == "Ground")
    {
    Player.GetComponent<Move2D>().isGrounded = false;
    }
    }
    }}

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grounded : MonoBehaviour
    6. {
    7.     GameObject Player;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Player = gameObject.transform.parent.gameObject;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.collider.tag == "Ground")
    21.         {
    22.             Player.GetComponent<Move2D>().isGrounded = true;
    23.         }
    24.     }
    25.  
    26.  
    27.  
    28.  
    29.  
    30.     private void OnCollisionExit2D(Collision2D collision)
    31.     {
    32.         if (collision.collider.tag == "Ground")
    33.         {
    34.             Player.GetComponent<Move2D>().isGrounded = false;
    35.         }
    36.     }
    37. }}

    And I wonder what I did wrong.

    I'm using Unity version 2019.3.6

    I would be very grateful if you could help me ;D

    //Lukas
     
  2. LukasAlstrup

    LukasAlstrup

    Joined:
    Mar 18, 2020
    Posts:
    3
    My Move2D:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move2D : MonoBehaviour
    6.  
    7. {
    8.     public bool isGrounded = false;
    9.     public float moveSpeed = 5f;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    20.         transform.position += movement * Time.deltaTime * moveSpeed;
    21.     }
    22.  
    23.     void Jump()
    24.     {
    25.         if (Input.GetButtonDown("Jump"))
    26.         {
    27.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
    28.         }
    29.     }
    30. }
    31.  
    32.  


    and my Grounded:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grounded : MonoBehaviour
    6. {
    7.     GameObject Player;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Player = gameObject.transform.parent.gameObject;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.collider.tag == "Ground")
    21.         {
    22.             Player.GetComponent<Move2D>().isGrounded = true;
    23.         }
    24.     }
    25.  
    26.  
    27.  
    28.  
    29.  
    30.     private void OnCollisionExit2D(Collision2D collision)
    31.     {
    32.         if (collision.collider.tag == "Ground")
    33.         {
    34.             Player.GetComponent<Move2D>().isGrounded = false;
    35.         }
    36.     }
    37. }}
    38.  
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,003
    It's just a typo, you are missing a close bracket "}" around line 17 on the Update() method.
    Make sure your brackets match up, it looks like you have an extra at the bottom of the file as well.
     
    LukasAlstrup likes this.
  4. LukasAlstrup

    LukasAlstrup

    Joined:
    Mar 18, 2020
    Posts:
    3
    Thank you very much Zombiegorilla ;D
     
  5. Comthru

    Comthru

    Joined:
    Nov 15, 2021
    Posts:
    1
    Just made the same mistake as u do XD
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Resurrecting an old post to say you had a typo isn't helpful.
     
    Kurt-Dekker and MousePods like this.
  7. Frozen_Atlas

    Frozen_Atlas

    Joined:
    Mar 11, 2022
    Posts:
    20
    I have a similar problem but cannot find a typo
     
  8. Frozen_Atlas

    Frozen_Atlas

    Joined:
    Mar 11, 2022
    Posts:
    20
    Can I have help or be redirected to another post
     
  9. Frozen_Atlas

    Frozen_Atlas

    Joined:
    Mar 11, 2022
    Posts:
    20
    This is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class UIController : MonoBehaviour
    {

    public static UIController instance;

    private void Awake()
    {
    if (instance == null)
    {
    instance = this;
    DontDestroyOnLoad(gameObject);
    } else
    {
    Destroy(gameObject);
    }
    }

    public Slider healthSlider;

    public Image fadeScreen;

    public float fadeSpeed = 2f;
    private bool fadingToBlack, fadingFromBlack;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    if(fadingToBlack)
    {
    fadeScreen.color = new Color(fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, Mathf.MoveTowards(fadeScreen.color.a, 1f, fadeSpeed * Time.deltaTime));

    if(fadeScreen.color.a == 1f)
    {
    fadingToBlack = false;
    }
    } else if(fadingFromBlack)
    {
    fadeScreen.color = new Color(fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, Mathf.MoveTowards(fadeScreen.color.a, 0f, fadeSpeed * Time.deltaTime));

    if(fadeScreen.color.a == 0f)
    {
    fadingFromBlack = false;
    }
    }

    public void UpdateHealth(int currentHealth, int maxHealth)
    {
    healthSlider.maxValue = maxHealth;
    healthSlider.value = currentHealth;
    }

    public void StartFadeToBlack()
    {
    fadingToBlack = true;
    fadingFromBlack = false;
    }

    public void StartFadeFromBlack()
    {
    fadingToBlack = false;
    fadingFromBlack = true;
    }
    }
    }
     
  10. Frozen_Atlas

    Frozen_Atlas

    Joined:
    Mar 11, 2022
    Posts:
    20
    The public modifier was working at one point but when I went to reload my game it called me out on the last three voids.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    I'm going to close this thread. Please create your own threads for your own problems. Do not hijack/ressurect an old thread to do this. Also, if posting code, please use code-tags.

    Finally and more importantly, 2D is not UI. The UI systems is not only another team, it also has its own dedicated sub-forums here.

    That said, you're really talking about how to use C# which is simply scripting and typos.

    Thanks.
     
Thread Status:
Not open for further replies.