0

long story short, I am creating an object pooling system and in my script (see below), PickAxeTestManager, I am getting ERROR CS1501 "No overload for method OnTriggerEnter takes 0 arguments" on line 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...      
    }
}
}

Here is my ResetByWall script (below), I have this script because when my PickAxe hits the "South Wall", I want it to go back into the pooling system (but for now, I am just "Destroying" it lol until I get the logic figured out).

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);

    }

}
}

My question is: can the OnTriggerEnter take an argument that is not of "type collider" so that I can rid of this "Null Reference" ? I did a lot of research before I asked this question and I could not find anything!

Also, here is my PickAxePoolManager script below (this is the main backbone of everything I am working on right now), but I am sure the only parts of this script you MIGHT even need to look at are the methods, CreatePool and 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
    }   
}
}

If you need more details, just let me know. Thank you! :)

Stephen George
  • 27
  • 1
  • 11

1 Answers1

2

First,

if (ResetByWall.instance.OnTriggerEnter()) //ERROR CS1501 is here. "No overload for method OnTriggerEnter takes 0 arguments".

"OnTriggerEnter" does not return anything (void), so using it in an if block is not doable.

Second,

My question is: can the OnTriggerEnter take an argument that is not of "type collider" so that I can rid of this "Null Reference" ?

If you want to use Unity's internal system, it has to be "type collider". However, if your usage of "OnTriggerEnter" does not require to cooperate with Unity's internal physics system, then you can always do function overloading in 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;
}
Ramazan Kürkan
  • 156
  • 1
  • 10
  • Thank you so much! Sorry for the late response, I wasn't able to program much the past couple days. I figured out how to fix my problem on this certain part regarding my pooling system, but what you kindly provided me helped me. I still have a lot to work on regarding my pooling system but you did help me on this aspect. Thank you again! Have a great weekend :) @Ramazan Kürkan – Stephen George Jul 03 '16 at 12:02