Range Attack + Animation Script
Hi,
I want the units of my RTS Project to attack an enemy. Therefor I have a Unit as Parent with Body, Head and Weapon ans Childs. I want to aplly an ANimation to the weapon acording to the script logic. The animations are "Present", "Fire", "Reload".
Here's my C# Script so far:
public static void Attack(GameObject Enemy, ArrayList Current)
{
var Unit = Enemy.GetComponent<Unit>();
for(int i = 0; i < Current.Count; i++)
{
if (Unit.Alive)
{
GameObject Attacker = Current[i] as GameObject;
var Att = Attacker.GetComponent<Unit>();
Att.IsAttacking = true;
var Musket = Unit.transform.FindChild("Musket");
if (Att.IsAttacking && Att.CanFire) {
Musket.GetComponent<Unit>().anim.Play("Fire");
Unit.HP -= 10f;
Att.CanFire = false;
Debug.Log("Enemy Hit. " + Unit.HP + " HP remaining.");
} else if (!Att.CanFire)
{
// ReloadMusket(Att); Error CS1503 Argument 1: cannot convert from 'Unit' to 'UnityEngine.GameObject'
}
}
}
}
public static void ReloadMusket(GameObject Unit)
{
float time = 10.0f;
var Musket = Unit.transform.FindChild("Musket");
time -= Time.deltaTime;
if (time > 0)
{
Musket.GetComponent<Unit>().anim.Play("Reload");
}
else if (time < 0)
{
Musket.GetComponent<Unit>().CanFire = true;
Musket.GetComponent<Unit>().anim.Play("Present");
}
}
I also reciefe an Error when I try to Call the FUnction ReloadMusket -> Error behind it in the code. I've already tried to parse it to a GameObject with ReloadMusket(Att as GameObject)
but that throws also an Error
Cannot convert type 'Unit' to 'UnityEngine.GameObject' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
I hope you can help me with that. Additionally, I've added a Screenshot of the Inspector and Hierarchy