Hi everyone,
Firstly I truly want everyone to know I sincerely apologize for having the ask what may be such a simple question, but I can really use another few sets of eyes to help me with this. Here is my prb. I have a simple game, where objects fall down into a basket at the bottom, which the player controls. One type of object that falls into the basket increases the score, however if the player misses the object it will hit a trigger collider at the bottom which will first destroy the falling GO, and set the GUI score to add up. However I have “special” items that fall and if they are caught inside the basket I want them to reverse the score that is attached to the bottom trigger. I feel very confident that I’m close to having this correct, and the game will run and compile fine, however I get a “Null Reference Exception - Object reference not set to an instance of the object”, each time one of my “specials” contacts the trigger inside the basket. Can someone please take a look at this and tell me what I’ve got going wrong. I swear I’ve looked all over the place and tried my damnedest to figure this out on my own, however this is one prb that has stumped me honestly for the last 5hrs or so…(and I did have to walk away for awhile in between all that time lol). Thanks everyone so much!
Error comes from line 58 of BasketTriggerControl.____________________________
using UnityEngine;
using System.Collections;
public class BasketTriggerControl : MonoBehaviour {
private int myScore = 0;
private int myScoreToLevel = 0;
private int myLevelScore = 0;
public TextMesh myScoreText;
public TextMesh myLevelText;
public TextMesh myEggScoreDown;
private bool canLevel = false;
private DestroyBottomController dbc;
void Start()
{
dbc = GetComponent<DestroyBottomController>();
}
void Update(){
if (canLevel == true)
{
UpLevel();
}
}
void OnTriggerEnter2D(Collider2D objects)
{
if(objects.gameObject.tag == "eggs")
{
Destroy(objects.gameObject);
myScore++;
myScoreToLevel++;
myScoreText.text = "Eggs = " + myScore;
}
if(objects.gameObject.tag == "chicks")
{
Destroy(objects.gameObject);
dbc.broEggScore--; <--ERROR HERE
//Debug.Log (dbc.broEggScore);
myEggScoreDown.text = "" + dbc.broEggScore;
}
}
Script I need to change variable on.____________________
using UnityEngine;
using System.Collections;
public class DestroyBottomController : MonoBehaviour {
public int broEggScore; <--VAR I'M WORKING WITH
public TextMesh broEggText;
void OnTriggerEnter2D(Collider2D collisionObject) {
Destroy (collisionObject.gameObject);
broEggScore++; <-- ADDS SCORE HERE
broEggText.text = "" + broEggScore;
}
}