- Home /
Enemy prefab wont work with the enemy movement script
This game is a 2d game in c#. My enemy is in a prefab and I have a script that spawns the prefab into the game scene, the prefab holds this script and also the enemy movement script, however when the prefab spawns in the scene the player cannot hit the enemy prefab and the enemy will not chase the player, which is what the enemy movement script should do. I know that the enemy movement script works however because when I place an enemy in the game scene hierarchy with the enemy movement script, it chases the player and attacks them, and the player can kill the enemy, it's just that when the prefabs spawn they don't do anything and I have no idea why
Need more info. Show the inspector with a spawned prefab that is not working selected, if you can. $$anonymous$$ake sure the enemy movement script is visible in the inspected in the screenshot.
Answer by bhavinbhai2707 · Dec 22, 2017 at 09:52 PM
Please show us the inspector or spawned prefab n probably the enemy script
This is the inspector for the zombie prefab (for some reason my player cannot be dragged into the variable unless it is a prefab, but even then, when this prefab is spawned it doesn't do what the enemy movement script tells it to
and here is my enemy movement script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy$$anonymous$$ovement : $$anonymous$$onoBehaviour {
//Who we are chasing
public Transform Player;
public Transform Zombie;
// how fast we want the enemy to chase
public float ChaseSpeed = 1f;
// the range at which it detects Player
public float detectRange = 10f;
public float attackRange = 1f;
// what the current speed is (get only)
float CurrentSpeed;
public float zombieHealth = 10f;
Player$$anonymous$$ovement player$$anonymous$$oveScript;
void Start ()
{
}
void Update()
{
if (Vector3.Distance(transform.position, Player.position) <= detectRange)
{ //check the distance between this game object and Player and continue if it's less than Range
CurrentSpeed = ChaseSpeed * Time.deltaTime; // set the CurrentSpeed to ChaseSpeed and multiply by Time.deltaTime (this prevents it from moving based on FPS)
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, Player.position, CurrentSpeed); // set this game objects position to the Player's position at the speed of CurrentSpeed
}
if(Vector3.Distance(transform.position,Player.position) <= attackRange)
{
// code to attack the player
//Debug.Log("Hitting Player");
player$$anonymous$$oveScript = GameObject.Find ("Player").GetComponent<Player$$anonymous$$ovement> ();
player$$anonymous$$oveScript.ApplyHealthDamage (1);
}
}
public void TakeHealthDamage(float enemyDamageTaken)
{
zombieHealth = zombieHealth - enemyDamageTaken;
Debug.Log ("YOU HURT THE ENE$$anonymous$$Y" + zombieHealth);
if (zombieHealth <= 0)
{
Debug.Log ("ENE$$anonymous$$Y DEAD");
Destroy(this.gameObject);
}
if (Zombie == null)
{
Debug.Log ("The enemy object is null");
}
}
}
Answer by mani3307 · Dec 23, 2017 at 03:12 PM
@unity_xEpj5-EHQg5OuA in your inspector window reference to your player's transform is missing; After solving this if your problem still persists create an empty game object in your hierarchy attach your "enemy spawn script " to it. and then attach the "enemy movement script" to the prefab
now i think it will work fine.
Answer by Larry-Dietz · Dec 23, 2017 at 06:35 PM
One thing you can do, assuming there is only one object in the scene with a Player script assigned to it, would be to add the following in your Start routine of the Enemy Movement script.
Player=FindObjectOfType<Player>().transform;
This will find an instance of the Player script somewhere in your scene, and assign it's transform to your Player variable, on start of the Enemy Movement script.
Hope this helps, -Larry