1400/4/23، 07:40 عصر
با سلام خدمت دوستان
من در حال ساخت یک بازی در یونیتی هستم که بصورت turn-based هست. موقع نوشتن اسکریپت به مشکلی خوردم که نتونستم حلش کنم. اروری که باهاش مواجه هستم NullReferenceException: Object reference not set to an instance of an object این هستش. وقتی که میرم توی اسکریپت تا ببینم مشکل کجاست لاین if(pathIndex == path.Count) رو نشون میده. سعی کردم که path رو از null بودن در بیارم ولی نتونستم. لطفا کمکم کنین تا مشکل رو حل کنم. کدی که نوشتم :
من در حال ساخت یک بازی در یونیتی هستم که بصورت turn-based هست. موقع نوشتن اسکریپت به مشکلی خوردم که نتونستم حلش کنم. اروری که باهاش مواجه هستم NullReferenceException: Object reference not set to an instance of an object این هستش. وقتی که میرم توی اسکریپت تا ببینم مشکل کجاست لاین if(pathIndex == path.Count) رو نشون میده. سعی کردم که path رو از null بودن در بیارم ولی نتونستم. لطفا کمکم کنین تا مشکل رو حل کنم. کدی که نوشتم :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
namespace SA.TB
{
public class UnitController : MonoBehaviour
{
List<PathInfo> path;
int pathIndex;
float moveT;
float rotateT;
float speedActual;
Vector3 startPosition;
Vector3 targetPosition;
bool initLerp;
public bool moving;
public int actionPoints = 20;
public float walk_speed = 2;
public float rot_speed =8;
public Node node
{
get
{
return GridBase.singleton.GetNodeFromWorldPosition(transform.position);
}
}
Node prevNode;
public int x1, z1;
Animator anim;
public void Init()
{
Vector3 worldPos = GridBase.singleton.GetWorldCoordinatesFromNode(x1, 0, z1);
transform.position = worldPos;
node.isWalkable = false;
node.ChangeNodeStatus(false);
anim = GetComponentInChildren<Animator>;();
anim.applyRootMotion = false;
}
void Update()
{
if(moving)
{
MovingLogic();
anim.SetFloat("vertical", 1, 0.2f, Time.deltaTime);
}
else
{
anim.SetFloat("vertical", 0, 0.4f, Time.deltaTime);
}
}
void MovingLogic()
{
if(!initLerp)
{
if(pathIndex == path.Count)
{
moving = false;
return;
}
node.ChangeNodeStatus(true);
moveT = 0;
rotateT = 0;
startPosition = this.transform.position;
targetPosition = path[pathIndex].targetPosition;
float distance = Vector3.Distance(startPosition, targetPosition);
speedActual = walk_speed / distance;
initLerp = true;
}
moveT += Time.deltaTime * speedActual;
if(moveT > 1)
{
moveT = 1;
initLerp = false;
RemoveAP(path[pathIndex]);
if(pathIndex < path.Count - 1)
pathIndex++;
else
moving =false;
}
Vector3 newPos = Vector3.Lerp(startPosition, targetPosition, moveT);
transform.position = newPos;
rotateT += Time.deltaTime * rot_speed;
Vector3 lookDirection = targetPosition - startPosition;
lookDirection.y = 0;
if(lookDirection == Vector3.zero)
lookDirection = transform.forward;
Quaternion targetRot = Quaternion.LookRotation(lookDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateT);
}
void RemoveAP(PathInfo p)
{
actionPoints -= p.ap;
node.ChangeNodeStatus(false);
}
public void AddPath(List<PathInfo> p)
{
pathIndex = 1;
path = p;
moving = true;
}
}
}
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
namespace SA.TB
{
public class UnitController : MonoBehaviour
{
List<PathInfo> path;
int pathIndex;
float moveT;
float rotateT;
float speedActual;
Vector3 startPosition;
Vector3 targetPosition;
bool initLerp;
public bool moving;
public int actionPoints = 20;
public float walk_speed = 2;
public float rot_speed =8;
public Node node
{
get
{
return GridBase.singleton.GetNodeFromWorldPosition(transform.position);
}
}
Node prevNode;
public int x1, z1;
Animator anim;
public void Init()
{
Vector3 worldPos = GridBase.singleton.GetWorldCoordinatesFromNode(x1, 0, z1);
transform.position = worldPos;
node.isWalkable = false;
node.ChangeNodeStatus(false);
anim = GetComponentInChildren<Animator>;();
anim.applyRootMotion = false;
}
void Update()
{
if(moving)
{
MovingLogic();
anim.SetFloat("vertical", 1, 0.2f, Time.deltaTime);
}
else
{
anim.SetFloat("vertical", 0, 0.4f, Time.deltaTime);
}
}
void MovingLogic()
{
if(!initLerp)
{
if(pathIndex == path.Count)
{
moving = false;
return;
}
node.ChangeNodeStatus(true);
moveT = 0;
rotateT = 0;
startPosition = this.transform.position;
targetPosition = path[pathIndex].targetPosition;
float distance = Vector3.Distance(startPosition, targetPosition);
speedActual = walk_speed / distance;
initLerp = true;
}
moveT += Time.deltaTime * speedActual;
if(moveT > 1)
{
moveT = 1;
initLerp = false;
RemoveAP(path[pathIndex]);
if(pathIndex < path.Count - 1)
pathIndex++;
else
moving =false;
}
Vector3 newPos = Vector3.Lerp(startPosition, targetPosition, moveT);
transform.position = newPos;
rotateT += Time.deltaTime * rot_speed;
Vector3 lookDirection = targetPosition - startPosition;
lookDirection.y = 0;
if(lookDirection == Vector3.zero)
lookDirection = transform.forward;
Quaternion targetRot = Quaternion.LookRotation(lookDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateT);
}
void RemoveAP(PathInfo p)
{
actionPoints -= p.ap;
node.ChangeNodeStatus(false);
}
public void AddPath(List<PathInfo> p)
{
pathIndex = 1;
path = p;
moving = true;
}
}
}