访问自定义类的IEnumerable 时的NullReferenceException

问题描述 投票:1回答:1

目标

我正在使用Unity设计boid系统。我通过在“ Swarm”列表中加入一个波伊德进入对撞机来处理感知半径。为了找到每个物体的力,我需要循环浏览群表,访问“ Boid”类,并获取速度和位置。

问题

来自每个群实体的Boid类被添加到新列表,并传递给物理控制器。但是,在第96行抛出NullReferenceException。

NullReferenceException:对象引用未设置为对象的实例Boid.Alignment(System.Collections.Generic.IEnumerable`1 [T] boids)(在Assets / Scripts / Boid.cs:96)Boid.Update()(位于Assets / Scripts / Boid.cs:42)

经过测试,似乎在访问新的Boid列表的任何部分时都会抛出它。

为什么我的新列表不包含任何数据?有没有更好的方法来处理3D空间中boid的2D实现?有没有可以用来更好地了解Linq的资源?

P.S。我对使用Linq系统非常陌生,大部分代码摘自this videothis Unity project script

代码

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

public class Boid : MonoBehaviour
{
// Global Variables
public Boid_Settings settings;

// Local Variables
public Rigidbody body;
public Vector2 acceleration;
public Vector2 velocity
{
    get
    { return new Vector2(body.velocity.x, body.velocity.z); }
    set
    { body.velocity = new Vector3(value.x, body.velocity.y, value.y); }
}
public Vector2 position
{
    get
    { return new Vector2(transform.position.x, transform.position.z); }
}
public List<GameObject> swarm = new List<GameObject>();
public List<GameObject> targets = new List<GameObject>();


// Functions
private void Start()
{
    float angle = Random.Range(0, 2 * Mathf.PI);
    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
}

private void Update()
{
    IEnumerable<Boid> boids = swarm.Select(o => o.GetComponent<Boid>()).ToList();

    Vector2 alignment = Alignment(boids);
    Vector2 separation = Separation(boids);
    Vector2 cohesion = Cohesion(boids);

    acceleration = settings.alignmentWeight * alignment + settings.cohesionWeight * cohesion + settings.seperationWeight * separation;

    UpdatePhysics();
}

// Entity Awareness Assignment
private void OnTriggerEnter(Collider collider)
{
    if (collider.CompareTag("Zombie"))
    { swarm.Add(collider.gameObject); }
    else if (collider.CompareTag("Player") || collider.CompareTag("Lure"))
    { targets.Add(collider.gameObject); }
}

private void OnTriggerExit(Collider collider)
{
    if (collider.CompareTag("Zombie"))
    { swarm.Remove(collider.gameObject); }
    else if (collider.CompareTag("Player") || collider.CompareTag("Lure"))
    {
        targets.Remove(collider.gameObject);
        StartCoroutine(LingerTarget(collider.gameObject));
    }
}

IEnumerator LingerTarget(GameObject target)
{
    targets.Add(target);
    yield return new WaitForSeconds(settings.lingerTime);
    targets.Remove(target);
}


// Core Boid Logic
public void UpdatePhysics()
{
    // Apply the acceleration, and then limit the speed to the maximum.
    Vector2 UncappedVelocity = velocity + acceleration;
    velocity = ApplyLimit(UncappedVelocity, settings.maxSpeed);

    float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
    body.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}

private Vector2 Alignment(IEnumerable<Boid> boids)
{
    Vector2 velocity = Vector2.zero;
    if (!boids.Any()) return velocity;

    foreach (Boid boid in boids)
    { velocity += boid.velocity; }
    velocity /= boids.Count();

    Vector2 steer = Steer(velocity.normalized * settings.maxSpeed);
    return steer;
}

private Vector2 Cohesion(IEnumerable<Boid> boids)
{
    if (!boids.Any()) return Vector2.zero;

    Vector2 sumPositions = Vector2.zero;
    foreach (Boid boid in boids)
    { sumPositions += boid.position; }
    Vector2 average = sumPositions / boids.Count();
    Vector2 direction = average - position;

    Vector2 steer = Steer(direction.normalized * settings.maxSpeed);
    return steer;
}

private Vector2 Separation(IEnumerable<Boid> boids)
{
    Vector2 direction = Vector2.zero;
    boids = boids.Where(o => Vector3.Distance(o.transform.position, position) <= settings.avoidanceRadius);
    if (!boids.Any()) return direction;

    foreach (Boid boid in boids)
    {
        Vector2 difference = position - boid.position;
        direction += difference.normalized / difference.magnitude;
    }
    direction /= boids.Count();

    Vector2 steer = Steer(direction.normalized * settings.maxSpeed);
    return steer;
}

private Vector2 Steer(Vector2 desired)
{
    Vector2 steer = desired - velocity;
    steer = ApplyLimit(steer, settings.maxSteerForce);

    return steer;
}

// Calculation Helpers
private Vector2 ApplyLimit(Vector2 baseVector, float limit)
{
    if (baseVector.sqrMagnitude > limit * limit)
    { baseVector = baseVector.normalized * limit; }
    return baseVector;
}
}

Boid_Settings模块:

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

[CreateAssetMenu]
public class Boid_Settings : ScriptableObject
{
// Boid
public float maxSpeed = 5;
public float avoidanceRadius = 1;
public float maxSteerForce = 3;

public float lingerTime = 2.5f;
public float alignmentWeight = 1;
public float cohesionWeight = 1;
public float seperationWeight = 1;
public float targetWeight = 1;

// Spawner
public float awarenessRadius = 2.5f;
}
c# linq ienumerable unityscript boids
1个回答
0
投票

通过检查您的代码,我有两点要指出:

  1. 您两次声明velocity。再次作为您类的公共属性,然后再次作为Alignment方法内部的局部变量。

  2. 这是您的异常可能来自:

CODE:

public Rigidbody body; //not initialized to anything

public Vector2 acceleration;

public Vector2 velocity
{
    get
    { 
         return new Vector2(body.velocity.x, body.velocity.z); 
    }
    set
    {
         //Since body is never initialized, calling .velocity throws a null exception
         body.velocity = new Vector3(value.x, body.velocity.y, value.y); 
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.