Unity 3D(C#) - 如何连接两个以上用作触发器的盒子对撞机

时间:2017-06-07 15:55:48

标签: c# unity3d

当前错误:NullReferenceException:对象引用未设置为对象实例BackboardTrigger.OnTriggerEnter(UnityEngine.Collider altCollider)(在Assets / Scripts / BackboardTrigger.cs:10)

我相信我的问题不同于,什么是NullReferenceException,我该如何解决?,因为它需要一个狭隘的答案。另一篇文章提供了NullReferenceException的广泛范围,而我需要知道如何连接两个以上的触发器。

我是C#的新手,我正在尝试重新创建一个简单的篮球游戏。截至目前我正试图在球首先击中篮板并顺序进入篮筐时触发得分。该教程目前教授如何在球开始进入篮筐并到达其底部附近时触发得分。

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.

我的PrimaryTrigger脚本:

 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脚本:

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脚本:

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

 public class BackboardTrigger : MonoBehaviour {

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

任何形式的帮助将不胜感激。我在Unity的第一场比赛。

0 个答案:

没有答案