Unity 3D(C#) - How to connect more than two box colliders used as triggers?
Current error but not really my question : NullReferenceException: Object reference not set to an instance of an object BackboardTrigger.OnTriggerEnter (UnityEngine.Collider altCollider) (at Assets/Scripts/BackboardTrigger.cs:10)
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.
So basically:
1: Player scores 2 points if it simply goes through the hoop. 2: Player scores 1 point if it hits the backboard and then goes through the hoop. 3: Player does not score if the ball simply hits the backboard and does not go through the hoop.
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.
Answer by GoldenWay · Jun 11, 2017 at 03:04 AM
A few things I can think of that can improve this (sorry if you already did any of the below):
Whenever the ball touches the ground or whenever you're going to shoot again, have expectedcollider and possiblecollider reset so as not to confuse the game into thinking when certain conditions are met for one shot, those conditions will be met for the rest of the game.
So from what I can tell, with this code, you will get 1 point if the ball touched PrimaryTrigger and BackboardTrigger before touching the SecondairyTrigger and two points if you touched PrimaryTrigger and then SecondairyTrigger without touching BackboardTrigger, that is what you wanted right?
Your error is taking place in your Backboard script at line 10 I think, if you're using monodevelop, when in that script, click the play button and it will basically check for errors, which will show up in red, if you're able to do that could you show a picture of that? It would make it a bit easier for me to understand/figure out. :)
Hello @GoldenWay ,
Thanks for the response.
"So from what I can tell, with this code, you will get 1 point if the ball touched PrimaryTrigger and BackboardTrigger before touching the SecondairyTrigger and two points if you touched PrimaryTrigger and then SecondairyTrigger without touching BackboardTrigger, that is what you wanted right?"
That is what I want. With this code currently, only the primary trigger and secondary trigger accumulates points when they're hit in succession. The backboard trigger does nothing at the moment and I'm trying to correct that.
"Your error is taking place in your Backboard script at line 10 I think, if you're using monodevelop, when in that script, click the play button and it will basically check for errors, which will show up in red, if you're able to do that could you show a picture of that? It would make it a bit easier for me to understand/figure out. :)"
When you say click the play button I'm assu$$anonymous$$g you mean within unity.
Your answer
Follow this Question
Related Questions
C# OnTriggerEnter Issue 3 Answers
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers
Crane Pick up script - setting new parent 0 Answers
Access variable from different class 1 Answer
OnTriggerEnter Issue - Collider problem 0 Answers