Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Prefabs executing in a wrong way.

$
0
0
Well, I'd like to start this question notifying you that I am Brazilian, so my English is in progress. I've made a prefab and put some of them on the Scene while run a script. The script is responsible to destroy the enemy when a player stomps on a collider on his head (that collider is a child of the enemy). Here is the enemy script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Frog : MonoBehaviour { private Rigidbody2D rig; public Animator anim; public float speed; public Transform upside; public Transform underside; private bool colliding; public LayerMask layer; public AudioSource audioSource; public static Frog frog; // Start is called before the first frame update void Start() { rig = GetComponent(); anim = GetComponent(); frog = this; } // Update is called once per frame void Update() { rig.velocity = new Vector2(speed, rig.velocity.y); colliding = Physics2D.Linecast(upside.position, underside.position, layer); if(colliding) { transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y); speed = -speed; } } void OnCollisionEnter2D(Collision2D coll) { if(coll.gameObject.tag == "Player" && !FrogCollision.frogColl.isFrogDead) { audioSource.clip = Player.instance.deathClip; audioSource.Play(); GameController.instance.ShowGameOver(); Destroy(coll.gameObject); } } } And this is the child collider script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class FrogCollision : MonoBehaviour { public AudioClip frogDeath; public bool isFrogDead; public static FrogCollision frogColl; void Start() { frogColl = this; } void OnCollisionEnter2D(Collision2D coll) { if(coll.gameObject.tag == "Player") { Frog.frog.audioSource.clip = frogDeath; Frog.frog.audioSource.Play(); coll.gameObject.GetComponent().AddForce(Vector2.up * 10, ForceMode2D.Impulse); Frog.frog.speed = 0; Frog.frog.anim.SetTrigger("isDead"); Destroy(Frog.frog, 0.3f); isFrogDead = true; } } } The problem is when I stomp on an enemy with a bunch of enemies on the same Scene. Neither the stomped enemy gets destroyed, nor others enemies get destroyed. Just one of them stops walking and doing animations while I die. And I do not know what should I do. Could you help me? Please!

Viewing all articles
Browse latest Browse all 1488

Trending Articles