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. Dismiss Notice

The modifier "private" is not valid for this item

Discussion in 'Scripting' started by SamsonSmith, Dec 10, 2020.

  1. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    Hello, I'm very new to c# coding and have recently been trying to make a collision detector (so the game can end). I have made a script on unity that should detect when an object has collided with another object. when i finished writing the script, a error showed up saying that the modifier "private" is not valid for this item (which i have no idea what it means. Please help!

    this is the script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class GameOverDetector : MonoBehaviour
    {
    // Update is called once per frame
    void Update()
    {

    private void onControllerColliderHit(ControllerColliderHit hit);
    {
    if (hit.transform.tag == "Player")
    {
    MeteorManager.gameOver = true;
    }
    }
    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Your onControllerColliderHit method is declared within the Update method. Having inline methods is not impossible since some C# versions ago, however it's most likely not what you want. For this kind of method declarations, you cant make them private or public (because access modifiers simply make no sense in that context), which is what the compiler is complaining about.

    You probably just meant to use the predefined method OnControllerColliderHit. Note the capital O. Spelling and capitalization is increadibly important in programming. someIdentifier and SomeIdentifier are most definitely not the same thing. Also, you will want to place the OnControllerColliderHit method outside the Update function, but still inside the GameOverDetector class.
     
    Bunny83 and SamsonSmith like this.
  3. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    Ok, thanks for the help
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Right, proper code highlighting would have helped I guess ^^
     
    Yoreki likes this.