- Home /
anim.SetBool not working on cloned GameObjects
Hey, new to Unity2D. I currently working on creating a 2D sidescroller game. I am cloning an enemy in my game that when it take a hit it should set a boolean to true. The issue is that it will change the expression on the original object but not the the clone. Below is the script attached to the original object.
using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour {
public Transform sightStart, sightEnd, target;
public bool spotted=false;
public bool facingLeft= true;
public HealthScript character;
public ScoreScript characterA;
public Transform zombie;
public float velocity=2f;
RaycastHit2D WhatIHit;
public bool InRange;
Animator anim;
int EcurrentHealth =2;
void Start(){
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
Raycasting ();
}
void Raycasting(){
spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
if (spotted == true ) {
anim.SetBool ("Spotted",true);
} else {
anim.SetBool ("Spotted",false);
}
facingLeft = !facingLeft;
if (target.position.x < zombie.position.x) {
transform.eulerAngles = new Vector2 (0, 180);
rigidbody2D.velocity = new Vector2 (velocity * -1, rigidbody2D.velocity.y);// enemy is walks to the left
} else {
transform.eulerAngles = new Vector2 (0, 0);
rigidbody2D.velocity = new Vector2 (velocity, rigidbody2D.velocity.y);// enemy moving to the right
}
if (Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"))) {
WhatIHit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
InRange = true;
}
else
{
WhatIHit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
InRange=false;
}
}
void AttackPlayer(){
StartCoroutine (WaitSet ());
if (InRange == true)
{
character.ApplyDamage(1);
}
}
IEnumerator WaitSet(){
yield return new WaitForSeconds (1);
}
public void EnemyDamage (int enemyhit){
anim.SetBool ("IwasHit",true);
EcurrentHealth = EcurrentHealth - enemyhit;
if (EcurrentHealth <= 0) {
Destroy(GameObject.Find("ZombieA(Clone)"));
}
}
}
please post the code for how you are instantiating the clones for the game object
Answer by chintan_shroff · Feb 13, 2015 at 06:01 PM
Instead of using StartCoroutine you could use InvokeRepeating
void Start()
{
// So the EnemySpawn function is called after 3secs after which a new enemy is generated every 2 secs.
InvokeRepeating ("EnemySpawn",3,2F);
}
Instantiate new clones as Rigidbody2D.
void EnemySpawn()
{
Rigidbody2D enemyInstance;
enemyInstance = Instantiate(enemy, transform.position, Quaternion.identity) as Rigidbody2D;
}
Now you can reference these clones as gameObject.name = "Enemy(Clones)".Hope this helps!
yes, this is how I have been instantiating clones in my games. Considering the fact that I am very new myself, I believe there might be a better way to do this but the way I have implemented the code is also correct according to me. Would really help if you could correct me here.
Answer by bryansull · Feb 13, 2015 at 02:26 PM
This is my script for cloning, its attached to an empty game object in my scene
using UnityEngine; using System.Collections;
public class SpawnManager : MonoBehaviour { public GameObject enemy;
// Use this for initialization
void Start () {
StartCoroutine(EnemySpawn());
}
IEnumerator EnemySpawn(){
while (true) {
Instantiate(enemy, transform.position, Quaternion.identity);
yield return new WaitForSeconds(4f);
}
}
}