gpt4 book ai didi

c# - NullReferenceException 'Object reference not set to an instance of an object' 统一 2D

转载 作者:行者123 更新时间:2023-12-03 08:33:26 25 4
gpt4 key购买 nike

我收到了 NullReferenceException在这一行:

animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);

我该如何解决这个问题?

这是我的代码:

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

public class PatrolBehaviour : StateMachineBehaviour
{
private GameObject[] patrolPoints;

public float speed;

int randomPoint;

override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
patrolPoints = GameObject.FindGameObjectsWithTag("patrolPoints");
randomPoint = Random.Range(0, patrolPoints.Length);
}

override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);

if (Vector2.Distance(animator.transform.position, patrolPoints[randomPoint].transform.position) < 0.1f)
{
randomPoint = Random.Range(0, patrolPoints.Length);
}
}

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{

}

}

最佳答案

错误“对象引用未设置到对象的实例”意味着您正在尝试访问当前为空的内容。要首先尝试解决此问题,请确保所有变量都已设置值并且不为空。

关于c# - NullReferenceException 'Object reference not set to an instance of an object' 统一 2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64589974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com