Hello, I’m working on a prototype for a class where a player can move around a box and collect balls for points. (I don’t even have that script working… but another thread for another time.) This code is to make the enemy (a cube) attack the player when in range and when hit, I want it to destroy the player and produce the end of game sequence (aka, Game Over, with a button that asks if you want to play again and a button for the main menu.). The player will not be attacking back - pretty much a game of cat & mouse (I hope i said this correctly; pulled my 3rd all-nighter this week - kinda delirious…)
Problem:
Assets\Scripts\Enemy.cs(44,27): error CS1061: 'Transform' does not contain a definition for 'player' and no accessible extension method 'player' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private Transform player;
private float dist;
public float moveSpeed;
public float lookRadius = 300f;
private Collision col;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
dist = Vector3.Distance(player.position, transform.position);
// If inside the radius
if (dist <= lookRadius)
{
// Move towards the player
GetComponent<Rigidbody>().AddForce(transform.forward * moveSpeed);
// Attack
FaceTarget();
OnCollisionEnter(col);
}
}
// Point towards the player
void FaceTarget()
{
Vector3 direction = (player.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnCollisionEnter(Collision col)
{
if (col.transform.player == "Player")
{
Debug.Log("Player has died.");
Destroy(player);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
Thanks so much for your help.