Y
Y
Yugg02019-01-27 15:25:21
C++ / C#
Yugg0, 2019-01-27 15:25:21

Why does my script give an error on SetBool?

In my script for the animation of the enemy, it gives an error! The script does not see the SetBool type, although I did everything right! 5c4da2b71adf8349816640.png I will give a script and a screen!

using System.Collections;
using UnityEngine;
using UnityEngine.AI;

public class EnemyController : MonoBehaviour
{
    public NavMeshAgent agent;
    public Animation animator;
    private Transform player;
    private bool attack;

    private void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        StartCoroutine(findPath());
    }

    IEnumerator playerDetect()
    {
        while (true)
        {
            if (player == null)
                break;
            if (Vector3.Distance (transform.position, player.position) < 1f)
            {
                animator.SetBool("attack", true);
                player.SendMessage("damege");
            }
            yield return new WaitForSeconds (.3f);
        }
    }

    IEnumerator findPath()
    {
        while (true)
        {
            if (player != null)
            {
                agent.SetDestination(player.position);
                yield return new WaitForSeconds(2f);
            }
            else break;
        }
    }

    private void Update()
    {
        
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
p4p, 2019-01-28
@YugGO

The code is not working, it's obvious you don't understand what you're doing. You have a public Animation animator; although it should be public Animator animator; if you want to reference the Animator component. Moreover, this same component must be received in Start and only after that call animator.SetBool("attack", true)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question