-1
I made some Prefab and put some of them on the scene while running a script.
The script is responsible for destroying the enemy when a player steps on a Colisor in his head (that Colisor is the enemy’s son).
Here is the enemy script (Frog):
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; // Parte superior de colisão contra objetos. (Só comentei para vocês não se confundirem, pode ignorar isso.)
public Transform underside; // Parte inferior de colisão contra objetos. (Só comentei para vocês não se confundirem, pode ignorar isso.)
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<Rigidbody2D>();
anim = GetComponent<Animator>();
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); // Cria uma linha invisível na frente do inimigo para fazer um colisor. (Só comentei para vocês não se confundirem, pode ignorar isso.)
if(colliding) // Se colidiu com algo... (Só comentei para vocês não se confundirem, pode ignorar isso.)
{
transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y); // ...inverte o inimigo; e, (Só comentei para vocês não se confundirem, pode ignorar isso.)
speed = -speed; // inverte a velocidade. (Só comentei para vocês não se confundirem, pode ignorar isso.)
}
}
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 script of the Son Researcher (Frogcollision):
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<Rigidbody2D>().AddForce(Vector2.up * 10, ForceMode2D.Impulse);
Frog.frog.speed = 0;
Frog.frog.anim.SetTrigger("isDead");
Destroy(Frog.frog.gameObject, 0.3f);
isFrogDead = true;
}
}
}
The problem is when I step on an enemy with several enemies in the same scene. The trampled enemy is not destroyed, but another.
So, I keep stepping on the enemy and gives this error on the console:
Missingreferenceexception: The Object of type 'Audiosource' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not Destroy the Object. Frogcollision.Oncollisionenter2d (Unityengine.Collision2d Coll) (at Assets/Scripts/Frogcollision.Cs:21)
Could someone help me, please?
I got this script you sent me. It works, but it still has bugs. So I went to make the suggestion that I said, I put a tag on the child object and I checked the player script. Testing, I saw that the collision was not being made at all. I made sure that Colisor and the player were colliding, but still no log I put in the script.
– Carlos Santos
What kind of bugs?
– Poke Clash
It was bad not to give details, but explaining... The frog that I walk, in fact, destroys itself, but does not perform the death animation. Another frog in the scene is that performs such animation; and also, stop walking.
– Carlos Santos
If I change the line
Frog.frog.anim.SetTrigger("isDead");
forthis.transform.parent.gameObject.GetComponent<Animator>().SetTrigger("isDead");
, the frog does not execute the animation, but still stands still. There is some way to access a public variable using thethis.transform.parent.gameObject
?– Carlos Santos
I managed to solve!!! For me to get a variable of the parent component, I have to use this:
this.transform.parent.gameObject.GetComponent<[nome do script, no meu caso, Frog]>()
. That way I managed to get everyone destroyed without disturbing another enemy. Thanks for the help, Poke Clash!!!– Carlos Santos
Denada :). Good Luck!
– Poke Clash