Help with accessing scoring system variable C# plz.

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;

	}

}

The problem is that you’re calling Destroy(gameObject) before your script ends.

When you destroy the gameobject you destroy the script (which is still working) so the script can’t finish doing what you want it to do :slight_smile:

Just move Destroy(objects.gameObject); down to the end of that section, after your debug.Log call and score changes.

At the line whith the error is the only time you use the dbc variable. Which means that the dbc variable has not been assigned properly (dbc is null). You assign it in the start function with the GetConponent function, the GetComponent function gets a component attached to the object you use it from. I don’t think your DestroyBottomController script is attached to the same object as the BasketTriggerControl or else the two scripts would trigger at the same time and the bottomcontroller would move with the basket as well. I am assuming that they are actually two separate objects instead. I would suggest making the drc variable public and assign it in the inspector in the editor instead. You could also use the Find function to search for it.

Hey Hey…thanks so much guys. At last I finally found a way to resolve the matter.

I still don’t understand why I was getting a null reference on the var in the other script, but in order to get around the prb I simply created a method on the BottomController that would do what I want and then just called a SendMessage in the basketTrigger where I was getting the error. This resolved the issue. I know that it’s not the absolute most optimal way to do this, however so far I’ve got resources to spare at run-time…so I can take this happily. :smiley:

Thank you guys so much!