Hi so im making a combo when you click multiple times and when i play it says: NullReferenceException: Object reference not set to an instance of an object
WeaponController.Update () (at Assets/Scripts/WeaponController.cs:33)
heres the code:
[SerializeField] GameObject katana;
bool canAttack = true;
[SerializeField] float attackCooldown = 1.0f;
float nextFireTime = 0f;
int numberofClicks = 0;
float lastClickTime = 0;
float maxcombodelay = 1;
public bool isAttacking = false;
[SerializeField] AudioClip swordEffect;
AudioSource source;
Animator anim;
[SerializeField] TextMeshProUGUI beanSelect;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (canAttack)
{
swordAttack();
}
}
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit1"))
{
anim.SetBool("hit1", false);
}
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit2"))
{
anim.SetBool("hit2", false);
}
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit3"))
{
anim.SetBool("hit3", false);
}
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit4"))
{
anim.SetBool("hit4", false);
numberofClicks = 0;
}
if (Time.time - lastClickTime > maxcombodelay)
{
numberofClicks = 0;
}
if (Time.time > nextFireTime)
{
if (Input.GetMouseButtonDown(0))
{
swordAttack();
}
}
}
public void swordAttack()
{
if (beanSelect.enabled == true)
{
return;
}
isAttacking = true;
lastClickTime = Time.time;
numberofClicks++;
Animator anim = katana.GetComponent<Animator>();
if (numberofClicks == 1)
{
anim.SetTrigger("Attack");
}
numberofClicks = Mathf.Clamp(numberofClicks, 0, 3);
if (numberofClicks >= 2 && anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit2"))
{
anim.SetBool("hit2", false);
anim.SetBool("hit3", true);
}
if (numberofClicks >= 2 && anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit3"))
{
anim.SetBool("hit3", false);
anim.SetBool("hit4", true);
}
canAttack = false;
source.PlayOneShot(swordEffect);
StartCoroutine(resetAttack());
}