[英]Unity 3D C# Check for Vector3 in List
I'm using Argon Granberg's A* Pathfinding scripts and I'm trying to check if a list element is defined 我正在使用Argon Granberg的A * Pathfinding脚本,并且试图检查是否定义了列表元素
There is a list of Vector3 values stored as p.vectorPath variable. 有一个Vector3值列表,存储为p.vectorPath变量。 I'm trying to test if list elements are defined with
我正在尝试测试列表元素是否定义为
if ( p.vectorPath[myIndex] != null ) {
...
But I'm getting the error in Unity that testing a Vector3 != null will always = true. 但是我在Unity中遇到了一个错误,即测试Vector3!= null将始终= true。 So how can i test if this particular list index is defined?
那么,如何测试此特定列表索引是否已定义?
Thanks. 谢谢。
Vector3
is a value type ( struct
) and there is no such thing as an undefined (or null) value type (except if you make it nullable). Vector3
是一个值类型( struct
),没有诸如未定义(或null)值类型的东西(除非将其设置为可空值)。 Value types are initialized in the moment they are declared in c# - if you didn't provide an initial value, it has its default value ( Vector3.zero
in this case) 值类型在用C#声明时即被初始化-如果您不提供初始值,则它具有其默认值(在这种情况下为
Vector3.zero
)
OTOH, if what you want is to know if the list has any element at the position N
, you could just check if list.Count
is greater than N
OTOH,如果您想知道列表在位置
N
处是否有任何元素,则只需检查list.Count
是否大于N
As with the other answer Vector3
cannot be null as it is a value type. 与其他答案一样,
Vector3
不能为null,因为它是一种值类型。 You could declare the list with a Nullable
type of Vector3
like this; 您可以像这样使用
Vector3
的Nullable
类型声明列表;
public class Foo
{
public List<Vector3?> VectorPath;
}
Foo p = new Foo();
//...stuff...
if (p.VectorPath[someIndex].HasValue)
{
//Do things.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.