0

The current error : NullReferenceException: Object reference not set to an instance of an object BackboardTrigger.OnTriggerEnter (UnityEngine.Collider altCollider) (at Assets/Scripts/BackboardTrigger.cs:10)

I believe my question is different from, What is a NullReferenceException, and how do I fix it?, because it requires a narrow answer. The other post provides a broad scope of what a NullReferenceException is while I need to know how to connect more than two triggers.

I'm a novice in C# and I'm attempting to recreate a simple basketball game. As of now I'm trying to trigger a score for when the ball hits the backboard first and sequentially enters the hoop. The tutorial currently teaches how to trigger a score for when the ball begins to enter the hoop and reaches near the bottom of it.

Right now when the ball enters the hoop through the first trigger(PrimaryTrigger) and sequentially through the second(SecondaryTrigger) a score is triggered.

I'm trying to cause a different scoring action for when the ball hits the backboard first.

My PrimaryTrigger Script:

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

 public class PrimaryTrigger : MonoBehaviour {

     void OnTriggerEnter(Collider collider)
     {
         SecondaryTrigger trigger = GetComponentInChildren<SecondaryTrigger>();
         trigger.ExpectCollider(collider);
     }
 }

SecondaryTrigger Script:

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

 public class SecondaryTrigger : MonoBehaviour {

     Collider expectedCollider;
     Collider possibleCollider;

     public void ExpectCollider(Collider collider)
     {
         expectedCollider = collider;
     }

     public void PossibleCollider(Collider altCollider)
     {
         possibleCollider = altCollider;
     }

     void OnTriggerEnter(Collider otherCollider)
     {

         if(otherCollider == expectedCollider && otherCollider == possibleCollider)
         {
             ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
             scoreKeeper.IncrementScore(1);
         }

         else if(otherCollider == expectedCollider)
         {
             ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
             scoreKeeper.IncrementScore(2);
         }

     }
 }

BackboardTrigger Script:

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

 public class BackboardTrigger : MonoBehaviour {

     void OnTriggerEnter(Collider altCollider)
     {
         SecondaryTrigger newTrigger = GetComponent<SecondaryTrigger>();
         newTrigger.PossibleCollider(altCollider);
     }
 }

Any form of help would be greatly appreciated. My first game in Unity.

Johnny Q
  • 1
  • 1
  • 5
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hellium Jun 07 '17 at 15:58
  • Welcome to StackOverflow, please, take some time to read the [HelpCenter](https://stackoverflow.com/help). I'm afraid your question will be closed soon because similar question has been asked before. – Hellium Jun 07 '17 at 16:00
  • The error is coming from the `BackboardTrigger` script. You need to attach `SecondaryTrigger` to the-same GameObject the `BackboardTrigger` script is attached to. If `SecondaryTrigger` is already attached to another GameObject, you just need to find that GameObject then get the component from it. `SecondaryTrigger newTrigger = GameObject.Find("OtherObject").GetComponent();` – Programmer Jun 07 '17 at 16:06
  • @Programmer Thanks for your help. Why wouldn't I need to search for a game object with the primary trigger. The primary and secondary works fine with one another. By the way they're all box colliders listed within the hierarchy of the same game object. – Johnny Q Jun 07 '17 at 16:36
  • 1
    I am sorry I don't understand what you are saying. Just try what's in my first comment. I also think that you should follow one of [Unity's tutorial](https://unity3d.com/learn/tutorials) to fully understand how components work in Unity. – Programmer Jun 07 '17 at 16:52
  • @Programmer Well thanks for the help again. I can't try what you suggest because I don't know what game object you're referring to. – Johnny Q Jun 07 '17 at 17:32
  • I am talking about the GameObject the `SecondaryTrigger` script is attached to. – Programmer Jun 07 '17 at 17:41
  • @Programmer What makes you say that the 'SecondaryTrigger' is attached to a GameObject? – Johnny Q Jun 07 '17 at 18:08
  • Ok. Watch one of the project tutorials I linked. You must understand basic Unity stuff. You don't at this moment. – Programmer Jun 07 '17 at 18:14
  • @Programmer So I suppose you don't believe that it's possible you're wrong in your thinking because again I don't have to do what you're saying with the PrimaryTrigger. – Johnny Q Jun 07 '17 at 18:23
  • Also I agree that I should review the Unity Tutorials but I thought I could use this resource to quicken the process. Seems like a simple overthought or one step is missing on my part here. – Johnny Q Jun 07 '17 at 18:26

0 Answers0