0
votes

I have set a parameter in Unity called DoAttack. This should trigger the attack animation when the spacebar is being pressed. (see Image underneath)

https://i.imgur.com/q44dPtq.jpg"> Parameter Image

Altho every time I press the attack animation, I keep getting the error "Parameter ' DoAttack' does not exist Unityengine.Animator:SetTrigger(String)"

I have created multiple scripts for my character. Within these scripts I have created a void telling what the character should do when a button is being pressed.

Underneath you can see the character base controls.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterBaseControl : MonoBehaviour
{
    private CharacterMovementModel m_MovementModel;
    private CharacterInteractionModel m_InteractionModel;
    private CharacterMovementView m_MovementView;

    void Awake() //Awake is similar to start, but awake is called before start. Make sure everything is set up in awake so you can start it in start.
    {
        m_MovementModel = GetComponent<CharacterMovementModel>();
        m_MovementView = GetComponent<CharacterMovementView>();
        m_InteractionModel = GetComponent<CharacterInteractionModel>();
    }

    protected void SetDirection( Vector2 direction )
    {
        if( m_MovementModel == null )
        {
            return;
        }
        m_MovementModel.SetDirection( direction );
    }

    protected void OnActionPressed()
    {
        if( m_InteractionModel == null )
        {
            return;
        }

        m_InteractionModel.OnInteract();
    }

    protected void OnAttackPressed()
    {
        if( m_MovementModel == null )
        {
            return;
        }

        if (m_MovementModel.CanAttack() == false )
        {
            return;
        }

        m_MovementModel.DoAttack();
        m_MovementView.DoAttack();
    }
}

To mix with this I have created a public bool where I return an attack state.

public bool CanAttack()
{
    return true;
}

public void DoAttack()
{
    Debug.Log( "Attack" );
}

Within the update function of the movement view (Where I can set the animation of the attack) I have created an animator bool updating the movement and the view of the animation during the attack.

void UpdateDirection()
{
    Vector3 direction = m_MovementModel.GetDirection();

    if ( direction != Vector3.zero )
    {
        Animator.SetFloat( "DirectionX", direction.x );
        Animator.SetFloat( "DirectionY", direction.y );
    }

    Animator.SetBool( "IsMoving", m_MovementModel.IsMoving() );
}

public void DoAttack()
{
    Animator.SetTrigger( " DoAttack ");
}

Yet everything I have done I keep getting the same message. I have looked all over my code but I cannot seem to find the mistake?


Character keyboard controls.

void Update()
{
    UpdateDirection();
    UpdateAction();
    UpdateAttack();
}

void UpdateAttack()
{
    if( Input.GetKeyDown( KeyCode.Space ) )
    {
        OnAttackPressed();
    }
}

void UpdateAction()
{
    if( Input.GetKeyDown( KeyCode.E ) )
    {
        OnActionPressed();
    }
}
1
Animator.SetTrigger( " DoAttack "); you have spaces here... I assume " DoAttack " should be "DoAttack".Johnny
@mafiaf You should remove spaces from " DoAttack "! Change Animator.SetTrigger( " DoAttack "); to Animator.SetTrigger("DoAttack");Ehsan Mohammadi
You are both right! I always put spaces in between so everything looks more organized., but since it's a name within Unity, it takes the space in the name as well. This seems to have fixed it thank you!mafiaf

1 Answers

0
votes

Looks like your answer was found in the comments, and it was a simple typo/spelling error.

Animator.SetTrigger( " DoAttack ");

Should now become...

Animator.SetTrigger("DoAttack");

Credit goes to Johnny and Ehsan Mohammadi for diving into this with the OP.