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 TheOatyBiscuit · Dec 31, 2012 at 06:05 PM · resetcollectible

Collectibles Objects and Resetting game after all are collected in javascript?

Hello I am a complete noob to scripting but I tried to make a script which would work for the following

There are 5 "notes" in game to collect. Once all 5 are collected the game goes back to the main menu. The notes are collected on collision with the character.

To set the script up I basically tried to set it to do what I was saying only in the script language but an error comes up saying "NullReferenceException: Object reference not set to an instance of an object."

The script starts:-

var CollectedObjects : int = 0;

var Note1 : GameObject;

var Note2 : GameObject;

var Note3 : GameObject;

var Note4 : GameObject;

var Note5 : GameObject;

I did this to decide that the initial collected object is set to 0 from the start and there are 5 to collect. Correct me if I am going down the wrong route.

I then put after the function update:-

function Update () {

 if(CollectedObjects >= 5)
 
 {
 
 Application.LoadLevel(0);
 
 CollectedObjects -= 5;
 
 }

}

I put this in so that if my character collects 5 notes that the game loads the main menu again. Again correct me if I have gone wrong here.

The final part of the script is:-

function OnControllerColliderHit (hit : ControllerColliderHit)

{

 if(hit.GameObject.tag == ("Note1"))
 
 {
 
     CollectedObjects += 1;
     
     Note1.active = false;
     
 }
 
 if(hit.GameObject.tag == ("Note2"))
 
 {
 
     CollectedObjects += 1;
     
     Note2.active = false;
     
 }
 
 if(hit.GameObject.tag == ("Note3"))
 
 {
 
     CollectedObjects += 1;
     
     Note3.active = false;
     
 }
 
 if(hit.GameObject.tag == ("Note4"))
 
 {
 
     CollectedObjects += 1;
     
     Note4.active = false;
     
 }                                
 
 if(hit.GameObject.tag == ("Note5"))
 
 {
 
     CollectedObjects += 1;
     
     Note5.active = false;
     
 }

}

This is to make it so that once the character and the note collide, it is collected and it adds one to the total collected objects as well as the note being removed from the level. Again correct me if I went wrong here.

When I am running the game it simply says the "NullReferenceException: Object reference not set to an instance of an object." error and it wont collect anything.

Please help as I have little scripting experience and I have the script copied up to a back-up file to try out any new ideas(however I cannot think of any) :)

Comment
Add comment · Show 6
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 TheOatyBiscuit · Jan 26, 2013 at 06:44 PM 0
Share

bump......

avatar image Doireth · Jan 26, 2013 at 10:12 PM 0
Share

Which line does the NullReferenceException occur? Will help narrow down the problem for us.

avatar image PH-zero · Jan 26, 2013 at 11:14 PM 0
Share

Do you use the Charactercontroller-gameObject? if yes, the 1st person, or the 3rd person?

avatar image FL · Jan 27, 2013 at 12:10 AM 0
Share

First, there is no reason to use tags if you made the notes as public variables. You can also usa Transform ins$$anonymous$$d of GameObject. Did you assigned in editor each of five notes?

PS: Use variables that starts with lowercase.

avatar image TheOatyBiscuit · Feb 28, 2013 at 10:22 PM 0
Share

Yes I use the CharacterController(1st person) from the standard assets. I have fixed the NullReferenceException with help from the answerer below(Codetastic) and also realised that I forgot some of the tags. However I still can't actually collect the notes.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Codetastic · Jan 27, 2013 at 02:12 AM

It looks like you over complicated things a bit too much. Rather than having a tag and a var for each note you can do it a fair bit simpler like this:

 var collectedObjects : int = 0;
 
 function Update(){
     if(collectedObjects >= 5){
         Application.LoadLevel(0);
         collectedObjects = 0;
     }
 }
 
 function OnControllerColliderHit(hit : ControllerColliderHit){
     if(hit.gameObject.tag == ("Note")){
         collectedObjects += 1;
         hit.collider.gameObject.active = false;
     }
 }

Now then, for this to work as intended you'll have to make a tag called "Note" and place that tag on each of your objects (the notes) that you want to collect. Also, each note will need a collider. Now as for the reason why your script did not work before I can't really say without seeing more info; but your script does not appear to have any issues inside the code. My guess would be that the variables Note1-Note5 were never set inside the inspector.

Comment
Add comment · Show 1 · 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 TheOatyBiscuit · Feb 28, 2013 at 10:04 PM 0
Share

Thank you for the improvements on the script, it will certainly help in future games with simplifying things a bit. I will check to see the issue you have suggested as well. Thank you again.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to reset scoreboard on death - help please 0 Answers

Collectible Items & Inheritance 0 Answers

Do global variables reset when you restart a scene? 1 Answer

How to import the object from server to unity 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