Compile Error issue

Hello all,

This was honesty my last resort I looked for answers on Google and Stack Overflow, but I haven’t found a solution to help me figure out what this compile error means and how to fix it. The Unity API.Vector3 says there must be 3 parameters, but it doesn’t say anything about error CS0426. Could someone let me know where I messed up if it’s not asking too much :smile:.

This code is for a little experiment I’m messing around with to make a skeleton move to random locations as pictured below. Once I slide the “player” cylinder into the skeleton’s collider he attacks like he should based off the code and anim controller, but he swings and still continues to move toward the random destination game object. So I’m trying to use a Vector3.MoveTowards to make the skeleton stop while the “player” is inside the collider, but I can’t clear this error to see if this Vector3.MoveTowards will even work.

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


public class RoamingSkeleton : MonoBehaviour
{
    public GameObject destinationLocation;
    public NavMeshAgent theAgent;
    public int Xpos;
    public int zPos;
    public Animator anim;
    public GameObject thePlayer;



    // Start is called before the first frame update
    void Start()
    {
        theAgent = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();

        theAgent.SetDestination(destinationLocation.transform.position);
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {  
            // Stop moving to destinationLocation and start attack animation at the players position.
            anim.SetBool("isWalking", false);
            theAgent.transform.position = new Vector3.MoveTowards(thePlayer.transform.position, transform.rotation);
            anim.SetBool("isAttacking", true);
        }
    }

    public void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {  
            // Start walking animation and go to destinationLocation.
            anim.SetBool("isWalking", true); 
            theAgent.SetDestination(destinationLocation.transform.position);
        }
    }
}

you cannot use new on MoveTowards
you have a working example here

next time try to understand the English portion of the error, we don’t care about the error codes that much. I may be a developer with many years of experience, but it’s not like I remember in my dream what CS02373542 means.

this error has nothing to do with Vector3, it has to do with how you’re abusing the language, not understanding what’s what. it’s just a general C# compile error that tells you that you’ve typed nonsense.

[further explanation]

" The type name ‘MoveTowards’ does not exist in ‘Vector3’ "

means that there is no TYPE called MoveTowards defined for Vector3, and you’ve surely tried to use it as such, by putting new in front of it. with new you define an instance of a type, allocate memory and get back a reference to it. you don’t use new with methods.

MoveTowards is just a static method. being static means that it is called from the Vector3 class directly, instead from one of its instances.

you also seem to have ignored its third argument, because this is the methods declaration.

public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
1 Like

Orionsyndrome,

Thank you for the explanation and clarification as to what I did wrong. I was under the impression that it was the Vector3, I wasn’t aware that I had typed a line of foolishness thanks for educating me man :smile:

1 Like