Hi guys i’ve got a trouble here… i’ve got an GO with a script on it that contains a bool, this GO has a child with the script that uses this bool to perform certain animation. When there is only 1 type of this GO everything is perf but when there are many of tham the sh&t happens… How to come over it?
Child’s script:
using UnityEngine;
using System.Collections;
public class AnimGuard : MonoBehaviour {
private AIscript boolholder;
private GameObject enemy;
// Use this for initialization
void Start () {
enemy = (GameObject) GameObject.FindWithTag ("enemy");
boolholder = (AIscript) enemy.GetComponent( typeof(AIscript) );
}
// Update is called once per frame
void Update () {
if(boolholder.iSeeYou == false){
animation.Play("idle");
}
if(boolholder.iSeeYou == true){
animation.Play("walk");
}
}
}
You’re using “findwithtag” which, as the description tells you, only finds the first object with the tag. You’re not guaranteed to get any particular object from it. Every object that has that script is using the same enemy object, so all the bools are the same.
You need to get enemy references from a list, or choose them in the inspector, or something. “FindWithTag” isn’t for things there are multiple of.
if this bool holder keep a bool of enemy and is in ur player object ?! 2 thing … first change your way and test this inside enemy script instead of player…
2. take arry of this bool with size of ur enemy count and also find ur enemy with “Objects” instead of “Object” or “FindsWithTag” instead of “FindWithTag” … i’m not which of these you sould replace to take answer cuz i did this long ago…
but u can do this with replace of of these… so u need arry of objects, cause it return multiple object… instead of one
then u can test bool for each enemy in for or foreach loop