The modifier 'private' is not valid for this item

Hello,
It’s my first time posting a thread so I hope I do nothing wrong.

I tried myself at scripting but I encountered an error and have no Idea how to fix it.
The Error: Assets\Scipts\Enemy.cs(45,11): error CS0106: The modifier ‘private’ is not valid for this item

The Code I wrote:

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

public class Enemy : MonoBehaviour
{

    public float speed;
    public Vector3 moveDirection;
    public float moveDistance;

    private Vector3 startPos;
    private bool movingToStart;

    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (movingToStart)
        {
            transform.position = Vector3.MoveTowards(transform.position, startPos, speed * Time.deltaTime);

            if (transform.position == startPos)
            {
                movingToStart = false;
            }
        }

        else
        {
            transform.position = Vector3.MoveTowards(transform.position, startPos + (moveDirection * moveDistance), speed * Time.deltaTime);

            if (transform.position == startPos + (moveDirection * moveDistance))
            {
                movingToStart = true;
            }
        }


          private void TriggerEnter(Collider other)
        {
            //If the collider tag is 'Player'...
            if (other.CompareTag("Player"))
            {
                //Call GameOver() that is inside "Player" class.
                other.GetComponent<Player>().GameOver();
            }

        }
    }
}

The code should normaly restart the scene if the player comes in contact with the enemy.

The problem seems to lie especially here:

 private void TriggerEnter(Collider other)
        {
            //If the collider tag is 'Player'...
            if (other.CompareTag("Player"))
            {
                //Call GameOver() that is inside "Player" class.
                other.GetComponent<Player>().GameOver();
            }

        }

I already tried removing private or moving it outside the update function. The Error is then gone but my player is still not dying on contact. I even tagged it with player.

Thank you beforehand for your answers.

I kinda found a solution? I think?

this is my new code:

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

public class Enemy : MonoBehaviour
{

    public float speed;
    public Vector3 moveDirection;
    public float moveDistance;

    private Vector3 startPos;
    private bool movingToStart;

    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.position;
  
  
    }

    // Update is called once per frame
    void Update()
    {
        if (movingToStart)
        {
            transform.position = Vector3.MoveTowards(transform.position, startPos, speed * Time.deltaTime);

            if (transform.position == startPos)
            {
                movingToStart = false;
            }
        }

        else
        {
            transform.position = Vector3.MoveTowards(transform.position, startPos + (moveDirection * moveDistance), speed * Time.deltaTime);

            if (transform.position == startPos + (moveDirection * moveDistance))
            {
                movingToStart = true;
            }
        }



    }

    private void OnTriggerEnter(Collider other)
    {
        //If the collider tag is 'Player'...
        if (other.CompareTag("Player"))
        {
            //Call GameOver() that is inside "Player" class.
            other.GetComponent<Player>().GameOver();
        }
    }



}

I put the private void OnTriggerEnter outside of the update function and then activated by my enemys the “Is Trigger” box under Box Collider and Capsule Collider. Is this mandatory? I don’t really understand it.

You can’t create a private function inside another function.

And in particular, it doesn’t make any sense to put the OnTriggerEnter() function inside the Update() function. If you’re unsure what this means, read up on C# classes and methods/functions, as this is pretty basic programming stuff.

1 Like

private, protected, public, and so forth are member access modifiers. They only make sense for members.

But what is is a member? The C# reference describes them pretty thoroughly. In short, members are anything that’s part of a class. Non-static members are associated with a specific instance of the class, and static members are associated with the class itself.

Therefore, it is bogus to try to use a member access modifier inside of a function: functions don’t have members!

More practically, though: you definitely want to have OnTriggerEnter() to be a member function of your class (i.e. not inside Update).

You can declare a function inside of another function. These are called local functions in C#. It’s handy if you want to do something several times, but also have no use for it outside of a specific context.

However, since a local function is not a member, Unity does not see it when checking if your class has a function called “OnTriggerUpdate”.

2 Likes