Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jamesster · Aug 23, 2010 at 12:03 AM · javascriptvariablescore

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Aug 23, 2010 at 12:23 AM 0
Share

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.

avatar image jamesster · Aug 23, 2010 at 12:28 AM 0
Share

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.

avatar image · Aug 23, 2010 at 12:31 AM 0
Share

Oops, had a typo in the second script. I was adding to 'collectibles' when I'd declared it as numCollectibles. Give that a try.

avatar image jamesster · Aug 23, 2010 at 12:41 AM 0
Share

Huh, it's still giving the same error. I've double checked, and everything is set up like you said...

avatar image · Aug 23, 2010 at 12:54 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Accessing Script From Other Script Causes Lag? 1 Answer

How would I be able to subtract from a variable when it hits something? (OnCollisionEnter) 1 Answer

Adding varibales from other scripts 3 Answers

Score Keeper / Kill Counter Help 1 Answer

Lower player's health from separate script. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges