- Home /
Collecting items doesn't reset when loading a new level?
As stated above I am having some issues with loading a level from collecting 3 items, here are the exact details:
First level - you collect 3 items and they display a HUD inventory as you collect them. Once you collect three the game changes to the next level - at this time - nothing resets, the hud is blank and you can no longer see your items being collected - everything else works / item collisions etc.
Here's my code:
ObjectCollect - on the gameobject / HUD
static var collect : int = 0;
var collect0tex : Texture2D;
var collect1tex : Texture2D;
var collect2tex : Texture2D;
var collect3tex : Texture2D;
var NameOfTargetLevel : String;
function NextLevel(){
//var collect : int = 0;
Application.LoadLevel(NameOfTargetLevel);
}
function Update () {
if(collect ==1) {
guiTexture.texture = collect1tex;
guiTexture.enabled = true;
}
else if(collect == 2){
guiTexture.texture = collect2tex;
}
else if(collect == 3){
guiTexture.texture = collect3tex;
NextLevel();
}
else {
guiTexture.texture = collect0tex;
}
}
this tells the game to display a hud and every time the character hits a trigger object with the 'pickup' script applied - the hud image will change / and on 3 will change to the target level.
Here is the pickup script:
//attach to object to pickup and collect points; set point value in hierarchy.
var points : int;
function Update () {
}
function OnTriggerEnter (col : Collider) {
var playerStatus : PlayerStatus = col.GetComponent(PlayerStatus);
playerStatus.AddPoints(points);
//Prints message in console (for debugging, not for game play):
//print ("Points added = " +points);
//function OnTriggerEnter(){
//TextHints.message = "Audio has been triggered.";
//TextHints.textOn = true;
ObjectCollect.collect++;
Destroy(gameObject);
}
Thanks for taking a gander at this if at all :) (sorry for the long post I wanted to be as specific as possible. Also I'm not a programmer so this sort of thing is tough for me to tackle off the bat)
highlight blocks of code and click the 010101 button to format them properly.
Answer by Peter G · Jul 26, 2011 at 09:23 PM
You need to remove the static keyword before collect
. Static fields won't change when you load a new level due to the fact that they are associated with the class itself, not an instance.
Thank you for the quick reply! So I gave this a try and now there's 2 new errors appearing in regards to the pickup script :
Assets/Scripts/2D/Pickup.js(27,15): BCE0020: An instance of type 'ObjectCollect' is required to access non static member 'collect'.
Assets/Scripts/2D/Pickup.js(27,15): BCE0049: Expression cannot be assigned to.
You need to find an instance of ObjectCollect
. The easiest way to do this is like such:
var objectCollect = FindObjectOfType(ObjectCollect) as ObjectCollect;
//Find an instance of ObjectCollect
objectCollect.collect++;
//Change the value of that instance
Answer by lizzyk21 · Jul 27, 2011 at 09:29 PM
I just want to say thank you Peter - you are a life saver!! Sorry for the silly questions concerning code - I'm more familiar with the visual development of games.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155655im_/https://answers.unity.com/themes/thub/images/avi.jpg)