- Home /
Keeping track of items
This is a very simple question, but I am new to Unity and JavaScript so I don't know how to do this. In my game, you can pick up items hidden in the levels that will unlock a bonus level when all 10 per level are collected - kind of like the Minikits or Artifacts in the LEGO games Travelers Tales has been making the past few years. I have made the following script:
var collectibles;
function OnTriggerEnter (object : Collider)
{
collectibles++;
Destroy (gameObject);
}
However, when I walk through the collectible, nothing happens. Unity then gives this error at the bottom of the screen:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) pickUpCollectible.OnTriggerEnter (UnityEngine.Collider object) (at Assets\Scripts\pickUpCollectible.js:4)
It says the error is on line 4. What am I doing wrong? I know this may sound stupid, but I can't figure it out. Thanks for the help! :)
Answer by · Aug 23, 2010 at 12:11 AM
You never initialised the value of collectibles (should be 0) and, assuming this script is on the collectible object, you're destroying the object that has the collectibles variable on it, so the value will never be saved.
You should declare a numCollectibles variable on a separate GameObject (maybe call it gameManager) and then add to it. For example, in a 'gameStats' script on the gameManager object, you would have:
static var numCollectibles : int = 0;
and then on the collectible gameObject (prefab, presumably):
function OnTriggerEnter (object : Collider)
{
gameStats.numCollectibles++;
Destroy(gameObject);
}
I can't test this from where I am, but hopefully it will give you what you're after.
I've just realised that you might have been putting the script on the Player object, in which case you need to 'Destroy(other.GameObject)' and don't need a static var to store the numCollectibles. If this is the case, let me know and I'll update the answer.
No, I was putting it on the collectible itself. However, I set things up like you said, and got this error: Assets/Scripts/pickUpCollectible.js(3,59): BCE0049: Expression cannot be assigned to.
Oops, had a typo in the second script. I was adding to 'collectibles' when I'd declared it as numCollectibles. Give that a try.
Huh, it's still giving the same error. I've double checked, and everything is set up like you said...
I haven't seen that error before, but I double checked my code and according to http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html, you don't need to Find() or GetComponent(). Updated the answer again ... I'll have access to Unity in about an hour, if it's still not working. Alternatively, you could set it up so the score system is kept on the player (rather than the collectible object), as per this answer here: http://answers.unity3d.com/questions/6571/collect-items-for-points