Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lizzyk21 · Jul 26, 2011 at 07:05 PM · resetcounterreloadbce0020bce0049

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)

Comment
Add comment · Show 1
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 almo · Jul 26, 2011 at 07:14 PM 0
Share

highlight blocks of code and click the 010101 button to format them properly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · 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 lizzyk21 · Jul 27, 2011 at 02:04 PM 0
Share

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.

avatar image Peter G · Jul 27, 2011 at 02:26 PM 0
Share

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
avatar image
0

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.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Counter doesn't reset 1 Answer

When ai touches gameobject reset scene. The code does not work any suggestions 2 Answers

I have a variable that does not re-set on the closing of my program, the variable is modified on the trigger enter, why does it not re-set? 0 Answers

Opened scene and now links to prefabs in inspector are gone 1 Answer

not resetting the death count on reload 5 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