Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 04, 2016 at 02:51 PM by stephen_george98 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stephen_george98 · Jun 29, 2016 at 05:44 PM · errorontriggerentersingletonnull reference exceptionobject pool

Null Reference When Calling OnTriggerEnter?

Hello all,

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by EDevJogos · Jun 29, 2016 at 07:49 PM

OnTriggerEnter needs a Collider as parameter.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stephen_george98 · Jun 29, 2016 at 08:26 PM 0
Share

So it HAS to take a parameter that is of type Collider? Do you know how I could get "(Collider other)" onto line 22 in my PickAxeTest$$anonymous$$anager script, if that's even possible? If that is not possible, maybe I should write a different way to be able to access other scripts' program besides the Singleton Pattern?

If I write "if (ResetByWall.instance.OnTriggerEnter(Collider))" I get 3 errors: Error CS0119, Error CS1502, and Error 1503.

If I write "if (ResetByWall.instance.OnTriggerEnter(Collider other))" I get 2 errors: Error CS1525 - unexpected symbol 'other', and Error CS8025 - parsing error. @Search

avatar image EDevJogos stephen_george98 · Jun 30, 2016 at 04:15 PM 1
Share

You can create an event on the script of your ResetByWall, something like

 public event Action<GameObject> onTriggerEntered;

 void OnTriggerEnter(Collider other)
 {
      if(onTriggerEntered != null) onTriggerEntered(other.gameObject);
 }

and then on your pickaxe just delegate it to a epecif method or a anomyous one:

 ResetBywall.instantece.onTriggerEntered += delegate(GameObject object) {  //code };
 

or

 ResetBywall.instantece.onTriggerEntered += NameOfThe$$anonymous$$ethod;
 


avatar image stephen_george98 EDevJogos · Jun 30, 2016 at 05:59 PM 0
Share

When I put:

 public event Action <GameObject> onTriggerEntered;
 
     void OnTriggerEnter(Collider other)
     {
         if (onTriggerEntered != null) 
         {
             onTriggerEntered (other.gameObject);
         }

I get Error CS0246: "The type or namespace name 'Action `1' could not be found. Are you missing a using directive or an assembly reference?" @Search

Show more comments
avatar image stephen_george98 · Jul 03, 2016 at 12:00 PM 0
Share

Sorry for the late response, but what you helped me out with did help on this certain part. Thank you so much! I still a good amount of functionality to work on regarding my pooling system, but you helped me on this one part so much. Have a great weekend :) @Search

avatar image
3

Answer by tanoshimi · Jun 29, 2016 at 08:41 PM

OnTriggerEnter is a function called by Unity when a rigidbody enters a collider marked as a trigger. You don't call it manually, so get rid of:

 if (ResetByWall.instance.OnTriggerEnter())

and put whatever logic you had following that into the OnTriggerEnter method itself.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Space Shooter: If that impact but not explode or ship or asteroids. 3 Answers

Play Animation on trigger enter 1 Answer

Endless Highway script CRASHES Unity!? 1 Answer

NullReferenceException on TransformArray 1 Answer

Where is this (-1) coming from in my change level script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges