- Home /
More than one gameObject on same script / The script only works for one gameobject and work on the rest of the gameobjects
I am trying to get all of the gameobject to play at the same time on the same script. I only gotten one gameobject to play in the scene. The other gameobject will stay on idle and wont play the rest of the animations . Here is my script :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform player;
static Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
}
public void Stop(){
anim.SetBool("isMoving", false);
anim.SetBool("isAttack", false);
anim.SetBool("isIdle", true);
this.enabled = false;
//Or if you want to destroy the AI script completely
//Destroy(this)
}
void Update ()
{
float speed = 0.1f;
this.transform.Translate(0,0,speed * Time.deltaTime);
if (Vector3.Distance(player.position, this.transform.position) < 25f)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
this.transform.Translate(0,0,0.09f);
anim.SetBool("isMoving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("isMoving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("isMoving",false);
anim.SetBool("isAttack",false);
}
}
}
Answer by StupidlySimple · Aug 06, 2016 at 01:25 PM
The problem might be here.
static Animator anim;
It's seem like every instance of the script is using the same component. Just change modifier to public or private. Also:
void Start()
{
anim = this.GetComponent<Animator>();
}
Your answer
Follow this Question
Related Questions
Components with same script destroyed when one of then is destroyed 1 Answer
Using same Animation to more things I want 2 Answers
How to prevent a script on a disabled object from firing? 0 Answers
How to access a game object from a different scene? 0 Answers
Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer