- Home /
Missing Variable for transform
So basically what I am trying to do is spawn in enemies that chase after my player. I have created a prefab and tested to make sure that the enemy actually chased the First Person Controller. However when I try to spawn the enemy I need a way to assign the Transform variable in the script. This kind of works but causes more errors and the enemy no longer follows.
#pragma strict
var Player : Transform;
var MoveSpeed = 4;
static var MaxDist = 10;
static var MinDist = 3;
static var isAttacking = false;
function Start ()
{
Player = Resources.Load("First Person Controller");
// this.animation.wrapMode = WrapMode.Loop;
}
function Update ()
{
if(Player == null)
{
print("Player is null");
}
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MinDist)
{
isAttacking = true;
}
else if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
isAttacking = false;
}
}
}
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.name == "Sword"){
isAttacking = true;
}
}
Answer by robertbu · Aug 01, 2013 at 05:39 PM
The typical way to do this is use have the player named or tagged 'Player'. If the player is named "Player", in start you do:
player = GameObject.Find("Player").transform;
or if use tagging:
player = GameObject.FindWithTag("Player").transform;
Both would be executed in Start().
Your answer
Follow this Question
Related Questions
,Getting a Spawner Manager Script to reference a Spawner 1 Answer
Only half of a prefab is spawning? 1 Answer
How do I get an object to Instantiate infront of a specific object/player? 1 Answer
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers
Spawn prefab only in allowed area? 1 Answer