- Home /
How do I play gameobjects in my scene from one script / Script is not working
I am using the same script on same gameobject. I had duplicated one gameobject in my scene . The problem I am having is the script the is not working the copy gameobject . The copy gameobject doesn't play any 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);
}
}
}
Try replace "static Animator anim" with "Animator anim". I.e, not to use static. It can help to play animations, because with "static" you have only one anim for all the instances of the game object.
It work. I got the animations to play. I have one more problem . When the enemy ai comes close the enemies go through each other . Do you know what the problem might be ?
Your answer
Follow this Question
Related Questions
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
GameObject is already being activated or desactivated 2 Answers
GameObject references runtime script in scene file. Unsure whats wrong 2 Answers
Saving objects through scenes 2 Answers
How can i use a script to automatic place invisible walls around the terrain edges ? 1 Answer