- Home /
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) :)
Which line does the NullReferenceException occur? Will help narrow down the problem for us.
Do you use the Charactercontroller-gameObject? if yes, the 1st person, or the 3rd person?
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.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613090203im_/https://answers.unity.com/themes/thub/images/avi.jpg)