How do I set a default target?
My "EnemyBasic" prefab is set to spawn in waves and successfully does but they need a target variable that is a transform type to have the "player" set, this way they move in a line towards the player. When they spawn, the player isn't set in the target variable because you can't save it in a prefab for some reason. Could I use a line of code in the wave spawn to set their target to the player? And if so, how? Thanks!
You can most certainly do this via code, but to give a more specific answer you should post some of your code.
var Distance; var Target : Transform; var lookAtDistance = 25.0; var chaseRange = 15.0; var attackRange = 1.5; var moveSpeed = 5.0; var Damping = 6.0;
var theDammage = 5;
var attackRepeatTime = 1; private var attackTime : float;
var Controller : CharacterController; var Gravity : float = 20; private var $$anonymous$$oveDurection : Vector3 = Vector3.zero;
function Start() { target = GameObject.Find("player"); attackTime = Time.time; Debug.Log("Start function has been run!"); }
function Update () {
if (Respawn$$anonymous$$enu.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
attack();
}
else if(Distance < chaseRange)
{
chase();
}
// if (Target != "player") // { // target = GameObject.Find("player"); // } } }
function lookAt() { renderer.material.color = Color.yellow; var rotation = Quaternion.LookRotation(Target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping); }
function chase() { renderer.material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= Gravity * Time.deltaTime;
Controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
function attack() { if (Time.time > attackTime) { Target.Send$$anonymous$$essage("ApplyDammage", theDammage, Send$$anonymous$$essageOptions.DontRequireReceiver); Debug.Log("The enemy has attacked"); attackTime = Time.time + attackRepeatTime; } }
function ApplyDammage() { chaseRange += 30; moveSpeed +=2; lookAtDistance += 40; }
Answer by PrisVas · Jun 28, 2016 at 07:48 PM
On the script that is attached to your "EnemyBasic" on the Start Function:
void Start() {
target = GameObject.Find("The Name Of Your Player");
}
That didn't work for me when I tried it. I put a Debug.Log after it and the line of code is being run, just not working right.
Answer by UrielKane · Jun 30, 2016 at 05:19 AM
Like PrisVas Say you should use a GamObject.Find() or GamObject.FindWithTag(). But what you are miss here is that the target variable is a transform type not a gameobject type. So to solve this problem you ned to get the compent via GetComponent.<>(); comandline. Something like this.
var target : Transform;
function Start () {
target = GameObject.Find("Player").GetComponent.<Transform>();
}
This should work.
Your answer
Follow this Question
Related Questions
Loading prefab 'sent' from other script? 2 Answers
No Script option in component Menu?! (Unity 5.3.1) 3 Answers
Prefab Scripts Not Working After First Spawn? 2 Answers
[HELP] Setting the parent of a transform which resides in a prefab is disabled 1 Answer
How to sync "cooldown timer" across multiple prefabs 0 Answers