im trying to set a target from a string or perhaps another way to set target when it loads in game heres my enemy attack script so far
the target im looking for is a gameobject my player character when i load in game
im able to add the target when game is running but i need it to auto add from the string name if possible
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
public string enemyattacktargetname;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AddjustCurrentHealth(-10);
}
if (target = null)
PlayerHealth.DontDestroyOnLoad(PlayerHealth);
}
}
}
↧