respawning enemies that recognize player
Trying to get more mileage from my prefab enemies. In their ATTACK scripts they seek out the player by his transform - a public variable I drag and drop "player" into and all is well. When I respawn that enemy that field is empty so he ignores me . How rude. Question is what do I need to do to this script so that field isn't empty when the enemy prefab spawns? specifically , the error line is
"if(Vector3.Distance(transform.position,Player.position)
this is where the enemy is suppose to tell if I am close enough to attack
public Transform Player;
is where I drag and drop the player into the script for the original enemy. but here's the entire script
using UnityEngine;
using System.Collections;
public class neoNaziAttack : MonoBehaviour {
public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
public GameObject enemy;
public Transform Player;//adds the player to the script
public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
//public int NavDist= 8;
public int NavSpeed= 8;
public float MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
public EnemyState currentState;
public GameObject getHim;
Animator anim;
public PlayerHealth playerHealth;
private NavMeshAgent agent;
public static bool PlayerDetected ;
public bool PlayerDetected2 ;
private bool jumping = false;//************this is the delay from hitreact
public float timeWhileJumping = 2f;
private bool jumping2 = false;//*****this is the delay from run to firing gun
public float timeWhileJumping2 = 2f;
public MonoBehaviour gun;
public GameObject naziWayOff;//turns off waypoints
void HitByRay () {
naziWayOff.SetActive (false);
anim.SetTrigger ("Hit");//ScoreManager.score += scoreValue;
StartCoroutine(DelayAfterJump());
agent.enabled = false;
transform.LookAt(Player);
anim.ResetTrigger ("walk");
anim.ResetTrigger ("idle");
anim.ResetTrigger ("idle2");
anim.ResetTrigger ("attack");
jumping = true;
}
void Start()
{
gun= enemy.GetComponent <naziRifle> ();
anim = enemy.GetComponent<Animator>();
agent = enemy.GetComponent<NavMeshAgent>();
playerHealth = GameObject.FindWithTag("Player").GetComponent <PlayerHealth> ();
//Player=Transform.FindWithTag("Player");
}
void Update()
{
PlayerDetected2 = PlayerDetected;
agent.enabled = true;
// if (currentState == EnemyState.ATTACK)
//agent.enabled = false;
if(PlayerDetected ==true)
MaxDist= 25;
if(PlayerDetected ==true)
naziWayOff.SetActive (false);
if(PlayerDetected ==true)
agent.speed = NavSpeed;
if(PlayerDetected ==true)
agent.stoppingDistance = MinDist;
if(PlayerDetected ==true)
agent.acceleration = 7;
;
if(playerHealth.currentHealth <= 0)
anim.SetTrigger ("idle");
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
{
PlayerDetected = true;
if (PlayerDetected)
{
transform.LookAt(Player);//Looks at the player
currentState = EnemyState.RUN;
}
}
else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
{
RoamAround ();
}
if (Vector3.Distance (transform.position,Player.position) < MinDist)
{
currentState = EnemyState.ATTACK;
Attack ();
}
if (currentState == EnemyState.IDLE)
{
gun.enabled = false;
RoamAround ();
}
else if (currentState == EnemyState.WALK)
{
anim.SetTrigger ("walk");
}
else if (currentState == EnemyState.RUN)
{
agent.SetDestination(getHim.transform.position);
agent.enabled = true;
anim.SetTrigger ("run");
anim.ResetTrigger ("attack");
anim.ResetTrigger ("idle");
gun.enabled = false;
anim.ResetTrigger ("stand");
}
else if (currentState == EnemyState.ATTACK)
{
//gun.enabled = true;
anim.ResetTrigger ("run");
}
else if (currentState == EnemyState.KILL)
{
//anim.Stop ("machinegunblast");
//GetComponent<Animation>().CrossFade (enemyKill.name);
}
}
void RoamAround()//__________________________________________________________________________________________________________________
{
gun.enabled = false;
naziWayOff.SetActive (true);
}
public void Attack()
{
anim.SetTrigger ("aim");
agent.enabled = false;
StartCoroutine(DelayAfterJump2());
anim.ResetTrigger ("run");
anim.ResetTrigger ("idle");
jumping2 = true;
if(playerHealth.currentHealth <= 0)
anim.SetTrigger ("idle");
if(playerHealth.currentHealth <= 0)
currentState = EnemyState.IDLE;
}
private IEnumerator DelayAfterJump(){
yield return new WaitForSeconds(timeWhileJumping);
MaxDist= 30;
anim.ResetTrigger ("Hit");
jumping = false;
agent.enabled = true;
agent.SetDestination(getHim.transform.position);
}
private IEnumerator DelayAfterJump2(){
yield return new WaitForSeconds(timeWhileJumping2);
gun.enabled = true;
jumping2 = false;
anim.ResetTrigger ("aim");
}
}
Answer by shadowpuppet · Jan 21, 2017 at 02:06 AM
so simple... remove the "public" option and just assign it in the start function;
Transform Player;
void Start()
{
Player=GameObject.FindGameObjectWithTag("Player").transform;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612105244im_/https://answers.unity.com/themes/thub/images/avi.jpg)