playerAnim.SetTrigger(“SwordAt”);
this codeline is wrong?
When i add , playerAnim.SetTrigger(“SwordAt”);
giving this error:
When i add , playerAnim.SetTrigger(“SwordAt”);
giving this error:
What does the console say?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
private float timeBtwAttack;
public float startTimeBtwAttack;
private Animator anim;
public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRange;
public int damage;
void Start(){
anim = GetComponent<Animator>();
anim.SetBool("SwordAt", false);
}
void Update(){
if(timeBtwAttack <= 0){
// then you can attack
if (Input.GetKey(KeyCode.Space)){
playerAnim.SetTrigger("SwordAt");
Collider2D[ ] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++)
{
enemiesToDamage*.GetComponent<Enemy>().TakeDamage(damage);*
*}*
*timeBtwAttack = startTimeBtwAttack;*
*}*
*} else {*
*timeBtwAttack -= Time.deltaTime;*
*}*
*}*
*void OnDrawGizmosSelected(){*
*Gizmos.color = Color.red;*
*Gizmos.DrawWireSphere(attackPos.position, attackRange);*
*}*
*}*
*```*
*Full code.*
Where is that playerAnim variable defined? I don’t see it.
Hi,
Where have you created your playerAnim? I think nowhere. You are trying to call that playerAnim but it’s not defined in your class.
So, if it’s of type Animator, add to the beginning of your script:
public Animator playerAnim;
Then you need to assign an animation in the Inspector to that slot.
And if you intend to use that Animator called anim, then you need to rename your playerAnim to anim on that line where the error happens.
this time not giving error but not working.
İf i delete this line
playerAnim.SetTrigger("SwordAt");
This code working but if i add playerAnim it’s not working.
Full code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
private float timeBtwAttack;
public float startTimeBtwAttack;
private Animator anim;
public Animator playerAnim;
public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRange;
public int damage;
void Start(){
anim = GetComponent<Animator>();
anim.SetBool("SwordAt", false);
}
void Update(){
if(timeBtwAttack <= 0){
// then you can attack
if (Input.GetKey(KeyCode.Space)){
Collider2D[ ] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++)
{
enemiesToDamage*.GetComponent<Enemy>().TakeDamage(damage);*
*}*
*timeBtwAttack = startTimeBtwAttack;*
*}*
*} else {*
*timeBtwAttack -= Time.deltaTime;*
*}*
*}*
*void OnDrawGizmosSelected(){*
*Gizmos.color = Color.red;*
*Gizmos.DrawWireSphere(attackPos.position, attackRange);*
*}*
*}*
*```*