Unity2d trying to make an enemy react to an attack with a flash. Why isn't this code working?
My first problem is getting this enemy to react. I'm sure I've gotta be doing something simple wrong. The if statement in the oncollision2d method is never being called. The information in that if statement isn't being printed.
The name of the asset that collides with the enemy is AttackSpellPrefab. There's a separate attack script that handles instantiating the attack and sending it along a path. in the hierarchy, when the attack instantiates, an item called AttackSpellPrefab(Clone) is created. Is that the correct name to reference in the above mentioned if statement? that script is attached to the player. the attack spell prefab is then dragged into a slot for a rigidbody2d on the player.
If it makes a difference on where I need to write the script, I would like to be able to switch between different attacks, so I think I would need to create a "spellbook" object that holds the master list of abilities (or just two to get the code going) and reference it from there, with the game manager script checking to see which spells the player has access to. when the player switches attacks, an object (the spellbook object?) should assign the correct image and rigidbody and collider to the player.
I can provide all of my code if you need more than just this class's code to help.
using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour {
Rigidbody2D myBody;
[SerializeField]
float hitPoints = 10; //tweakable number of hit points to initialize with
float currentHitPoints; //HP number to work with in script
Transform myTransform;
//[SerializeField]
// Sprite splatSprite; //sprite to indicate death
// [SerializeField]
//Sprite liveSprite; //sprite to indicate not dead.
SpriteRenderer mySprite;
// Use this for initialization
void Start() {
myBody = GetComponent<Rigidbody2D>();
currentHitPoints = hitPoints;
}
public void OnCollisionEnter2d(Collision2D other)
{
if (other.transform.name == "AttackSpellPrefab(Clone)")
{
currentHitPoints--;
FlashIt();
print("I've been hit!");
print(currentHitPoints);
}
}
IEnumerator FlashIt()
{
GetComponent<SpriteRenderer>().enabled = false;
yield return new WaitForSeconds(.5f);
GetComponent<SpriteRenderer>().enabled = true;
}
public void HealthCheck()
{
if (currentHitPoints < 1)
{
// mySprite.sprite = splatsprite
print("I Die");
}
}
// Update is called once per frame
void Update () {
HealthCheck();
}
}
first: put a print in top of OnCollisionEnter2d to see if it gets there "print(other.name)" the if statement looks good.
thanks for confirmation on the if statement. it is indeed not getting there. I have public void OnCollisionEnter2d(Collision2D other) { print(other.transform.name); if...
it didn't seem right to put it outside of the method, and it threw errors, so if I am supposed to place the print statement above the method, could you provide syntax? thanks again!
I moved that method right below the declaration of variables, which meant just moving the start method below the oncollisionenter2d method. (are they called methods? I've said it in my head so much it just sounds weird now.) but still no go. im going to investigate issues with my prefab.
Comparing with name is not a good idea when you are instantiating the objects. Ins$$anonymous$$d assign tag to the prefab and compare using tag like this:
if (other.gameObject.tag== "AttackSpellPrefab")
I was trying to make exactly that happen. I guess i was way off? I had [SerializeField] bool IsAttackSpell = true
in the script for the attack spell. on the attack spell prefab, this gives me a checkbox for that purpose. I couldn't figure out how to reference that variable in script. How wrong was I in doing it that way?
@UsmanAbbasi I assigned a tag to the prefab for the attack spell, but when it pulls the name in code, it is saying Untagged. I think it's trying to give me the players tag, since the player originates the attack. do I need to do something like gameObject.instantiatedObject.tag? it is only happening when my spell sprite hits the enemy sprite, and when I pause and step through the code, I see my tag on the instantiated clones.
Print(Debug.Log) the name of the gameobject whose tag you are accessing to make sure what gameobject it is checking.
Answer by andrew23890 · Jun 01, 2016 at 07:58 PM
To resolve my main issue i used a second hitbox and made it a trigger with no material, then used ontrigger instead of oncollision. I also assigned a tag to the attack and use the tag to identify the gameobject. It now calls the if statement, checks if it's an attack, and reduces the enemies health, prints the enemies health, declares it has been hit.
I'm now working on getting
IEnumerator FlashIt()
{
GetComponent<SpriteRenderer>().enabled = false;
yield return new WaitForSeconds(.5f);
GetComponent<SpriteRenderer>().enabled = true;
}
to work by calling FlashIt(); in the if statement in the ontriggerenter2d method.