Home > Software design >  Variable type "Action" not being "found"
Variable type "Action" not being "found"

Time:12-23

So essentially, I have a script using LeanTween to make an object follow a bunch of waypoints then loop back on itself. However, in the tutorial I followed the guy uses a variable of type "Action" which for some reason flags up an error for me (I think it's because I am using a newer version of Unity).

Therefore, can anyone suggest to me how I might fix this error and if not how I can form a work around? Thank you.

Error message: Assets\Moveable.cs(31,45): error CS0246: The type or namespace name 'Action' could not be found (are you missing a using directive or an assembly reference?)

Script which houses the error:

public class Moveable : MonoBehaviour
{
    [SerializeField] private float _speedMetersPerSecond = 25f;

    private Vector3? _destination;
    private Vector3 _startPosition;
    private float _totalLerpDuration;
    private float _elapsedLerpDuration;
    private Action _onCompleteCallback;

    // Update is called once per frame
    void Update()
    {
        if (_destination.HasValue == false)
            return;

        if (_elapsedLerpDuration >= _totalLerpDuration && _totalLerpDuration > 0)
            return;

        _elapsedLerpDuration  = Time.deltaTime;
        float percent = (_elapsedLerpDuration / _totalLerpDuration);
        Debug.Log($"{percent} = {_elapsedLerpDuration} / {_totalLerpDuration}");

        transform.position = Vector3.Lerp(_startPosition, _destination.Value, percent);
    }

    public void MoveTo(Vector3 destination, Action onComplete = null)
    {
        var distanceToNextWaypoint = Vector3.Distance(transform.position, destination);
        _totalLerpDuration = distanceToNextWaypoint / _speedMetersPerSecond;

        _startPosition = transform.position;
        _destination = destination;
        _elapsedLerpDuration = 0f;
        _onCompleteCallback = onComplete;
    }
}

Other script that may influence it:

public class MoverController : MonoBehaviour
{
    [SerializeField] private Moveable target;
    private List<Transform> _waypoints;
    private int _nextWaypointIndex;


    private void OnEnable()
    {
        _waypoints = GetComponentsInChildren<Transform>().ToList();
        _waypoints.RemoveAt(0);
        MoveToNextWaypoint();
    }

  private void MoveToNextWaypoint()
    {
        var targetWaypointTransform = _waypoints[_nextWaypointIndex];
        target.MoveTo(targetWaypointTransform.position, MoveToNextWaypoint);
        target.transform.LookAt(_waypoints[_nextWaypointIndex].position);
        _nextWaypointIndex  ;

if (_nextWaypointIndex >= _waypoints.Count)
            _nextWaypointIndex = 0;
        
    }
}

Screenshots of where the code is attached: Object for Moveable script Object for MoveController script

CodePudding user response:

If you add using System; at the top of your document, it should correctly reference System.Action<T>

  • Related