调用OnTriggerEnter时为空引用?

时间:2016-06-29 03:39:51

标签: singleton collision-detection nullreferenceexception unity5 object-pooling

长话短说,我正在创建一个对象池系统,在我的脚本中(见下文),PickAxeTestManager,我得到ERROR CS1501"没有重载方法OnTriggerEnter接受0个参数"在第22行。

using UnityEngine;
using System.Collections;

public class PickAxeTestManager : MonoBehaviour {

public GameObject PickAxeprefab;

void Start ()
{
    PickAxePoolManager.instance.CreatePool (PickAxeprefab, 2); //CreatePool is a method in PickAxePoolManager
}


void Update () 
{

    if (Input.GetKeyDown (KeyCode.A)) 
    {
        PickAxePoolManager.instance.ReuseObject(PickAxeprefab, Vector3.zero, Quaternion.identity); //ReuseObject is also a method in PickAxePoolManager 
    }

    if (ResetByWall.instance.OnTriggerEnter()) //ERROR CS1501 is here. "No overload for method OnTriggerEnter takes 0 arguments". 
    {
        PickAxePoolManager.instance.ReuseObject(PickAxeprefab, Vector3.zero, Quaternion.identity); // Same here...      
    }
}
}

这是我的ResetByWall脚本(下图),我有这个脚本,因为当我的PickAxe点击"南墙"时,我希望它回到池系统(但是现在,我只是"摧毁"它直到我弄清楚了逻辑)。

using UnityEngine;
using System.Collections;

public class ResetByWall : MonoBehaviour {

public bool collided;

//NOTE:
//Singleton Pattern from lines 12 to 25 //
// ************************************

static ResetByWall _instance; // Reference to the Reset By Wall script

public static ResetByWall instance  // This is the accessor
{
    get 
    {
        if(_instance == null)   // Check to see if _instance is null
        {
            _instance = FindObjectOfType<ResetByWall>(); //Find the instance in the Reset By Wall script in the currently active scene
        }

        return _instance;
    }
}

//public GameObject prefab;

public void OnTriggerEnter(Collider other) {

    collided = true;

    if (other.gameObject.tag == "Pick Axe_PoolerTest") //I tagged the prefab as "Pick Axe_PoolerTest"
    {
        Debug.Log("Pick Axe entered the trigger!");

        Destroy(other.gameObject);

    }

}
}

我的问题是:OnTriggerEnter可以采用不是&#34;类型对撞机&#34;的参数。这样我就可以摆脱这个&#34; Null Reference&#34; ?在我提出这个问题之前,我做了很多研究,我找不到任何东西!

另外,下面是我的PickAxePoolManager脚本(这是我现在正在处理的所有内容的主要内容),但我确信你可能需要查看的这个脚本的唯一部分是方法,CreatePool和ReuseObject。

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

public class PickAxePoolManager : MonoBehaviour {

Dictionary<int,Queue<GameObject>> poolDictionary = new Dictionary<int,Queue<GameObject>>();

//NOTE: 
//Singleton Pattern used from lines 12 to 25

static PickAxePoolManager _instance; // Reference to the Pool Manager script

public static PickAxePoolManager instance   // This is the accessor
{
    get 
    {
        if(_instance == null)   // Check to see if _instance is null
        {
            _instance = FindObjectOfType<PickAxePoolManager>(); //Find the instance in the Pool Manager script in the currently active scene
        }

        return _instance;
    }
}

/// <summary>
/// Creates the pool.
/// </summary>
/// <param name="prefab">Prefab.</param>
/// <param name="poolSize">Pool size.</param>

public void CreatePool(GameObject prefab, int poolSize)
{
    int poolKey = prefab.GetInstanceID ();  // Unique integer for every GameObject

    if (!poolDictionary.ContainsKey (poolKey))   //Make sure poolKey is not already in the Dictionary, 
        //if it's not then we can create the pool 
    {
        poolDictionary.Add(poolKey, new Queue<GameObject>());

        for (int i = 0; i < poolSize; i++)  //Instantiate the prefabs as dictated by the "poolSize" integer
        {
            GameObject newObject = Instantiate (prefab) as GameObject;  //Instantiate as a GameObject
            newObject.SetActive(false);  // Don't want it to be visible in the scene yet
            poolDictionary [poolKey].Enqueue(newObject);    // Add it to our Pool
        }
    }
}

/// <summary>
/// Reuses the object in our pool.
/// </summary>
/// <param name="prefab">Prefab.</param>
/// <param name="position">Position.</param>
/// <param name="rotation">Rotation.</param>

public void ReuseObject (GameObject prefab, Vector3 position, Quaternion rotation)
{
    int poolKey = prefab.GetInstanceID ();  // Get our pool key once again

    if (poolDictionary.ContainsKey (poolKey)) // Quick check to make sure our pool dictionary contains the pool key
    {
        GameObject objectToReuse = poolDictionary[poolKey].Dequeue(); //Get the next object in the pool
        poolDictionary[poolKey].Enqueue(objectToReuse); // Add the object back onto the end of the queue so it can be reused later

        objectToReuse.SetActive(true);  //Make sure the object was not disabled

        objectToReuse.transform.position = position; // set position to applied values
        objectToReuse.transform.rotation = rotation; // set rotation to applied values
    }   
}
}

如果您需要更多详情,请告诉我们。谢谢! :)

1 个答案:

答案 0 :(得分:2)

<强>第一

  

if(ResetByWall.instance.OnTriggerEnter())// ERROR CS1501在这里。 &#34;方法OnTriggerEnter没有重载需要0个参数&#34;。

&#34; OnTriggerEnter&#34;不返回任何内容(void),因此在if块中使用它是不可行的。

<强>第二

  

我的问题是:OnTriggerEnter可以采用不是&#34;类型对撞机&#34;的参数。这样我就可以摆脱这个&#34; Null Reference&#34; ?

如果你想使用Unity的内部系统,它必须是&#34;键入collider&#34;。但是,如果您使用&#34; OnTriggerEnter&#34;不需要与Unity的内部物理系统合作,那么你总是可以在C#中进行函数重载。

public boolean OnTriggerEnter(){
    // Internal logic
    return true/false;
}

public boolean OnTriggerEnter(Collider other){
    // Internal logic
    return true/false;
}

public boolean OnTriggerEnter(GameObject target){
    // Internal logic
    return true/false;
}