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.