- Home /
Question by
Josenifftodd · Dec 22, 2014 at 12:49 AM ·
javascriptaienemynot workingtarget
why won't my code work?
//#pragma strict
var enemies : Transform[];
var rand;
function start () {
enemies = GameObject.FindGameObjectsWithTag("Enemy");
transform.LookAt(enemies);
}
function Update () {
LookAtRandomEnemy();
}
function LookAtRandomEnemy(){
if (Input.GetButtonDown ("Fire1")){
Random.Range(1, enemies.Length);
}
Comment
look at your function start line line 4
capitalize your start to Start
this may be causing issues
Answer by Firedan1176 · Dec 22, 2014 at 01:23 AM
You can't do Transform.LookAt(enemies)
because it's an array, you need to grab one of the transforms out of the array and look at it (you can't look at all of them at once).
You need to do something with Random.Range, not just state it. Something like
float myFloat;
void Start() {
myFloat = Random.Range(0, enemies.Length);
}
void Update() {
Transform.LookAt(enemies[myFloat]);
}
I suggest you look at the documentation before asking at the unity answers.
Your answer
Follow this Question
Related Questions
Checking name from hit from Raycast in array 2 Answers
Collsions problem 0 Answers
Top Down Shooter Enemy AI using raycasting 1 Answer
Make enemy more intelligent 0 Answers
Attack/Targeting Script Issue 1 Answer