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
1
Question by Darksider · Dec 13, 2013 at 04:13 PM · beginnercounter

Counter problem

Hi. I am a beginner in Unity. I am doing the Challenge 03 (http://www.unity3dstudent.com/2010/07/challenge-c03-beginner/) and got some problems. I have written a script.

 var Counter :int =0;
 function Update(){
 var fwd =transform.forward*100;
 rigidbody.AddForce(fwd);
 }
 function OnCollisionEnter(theCollision:Collision){
 if (theCollision.gameObject.name=="smalltarget" ){
 Destroy(theCollision.gameObject);
 Destroy(gameObject);
    //not Destroy(gameObject.Find("smalltarget");
    Counter++;
    //tried also Counter+=1
 
 Debug.Log("smalltarget destroyed");
 }
 Debug.Log(Counter);
 if (Counter >=3){
 Debug.Log("now scene must be loaded");
 }
 }
 


The problem is that the Counter number remains 1 in Debug.Log and it is not rising. What should I do? Could you help me please?

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

Answer by KellyThomas · Dec 13, 2013 at 04:34 PM

  • What is the Log printing out?

  • Which GameObject/prefab is this script attached to?

  • Line 9: Destroy(gameObject); seems to be destroying the current object and by extension this instance of the script, if any other instances of this script exist on other gameObjects in your scene they will have their own counter variable, that will hold independent values.

Comment
Add comment · Show 12 · 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 Darksider · Dec 14, 2013 at 10:34 AM 0
Share

So... I made a box named smalltarget. $$anonymous$$ade it a prefab with the same name and put it in the scene three times. I made an empty game object, connected to main camera, that had the mooving script. On the empty game object, there was an instantiate script, that instantiated a box prefab, with this script on it. The idea is that when the box hits a smalltarget prefab, destroys it and destroys itself. A counter counts the number of collisions. If the counter number reaches 3, something is printed in debug.log.(that will be replaced with Application.LoadLevel(). ) Debug.Log(counter) shows me the number of hits, which is not raising, it remains 1.

avatar image KellyThomas · Dec 14, 2013 at 10:52 AM 0
Share

O$$anonymous$$ then:

Your counter is an instance variable, i.e. each instance has it's own independent counter initialized to 0, that is why it never seems to get past 1.

Try changing line 1 to:

 static var Counter :int = 0;

then running your game.

If you are then getting errors accessing Counter on lines 11, 16 & 17 you should adopt the ClassName.variableName format.

A quick write up and example is available here.

avatar image Darksider · Dec 14, 2013 at 06:19 PM 0
Share

$$anonymous$$aking that variable static worked. Thanks. But I can't understand what means static?

so...so far I understand trough : Class-a collection of fuctions. still

What is an instance variable? What is a static variable? An instance of a class is actually an element of that class? so example transform=class translate=instance of that class? Look.At()=other instance?

I am a little puzzled. I have never studied JavaScript before this PDF(http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf ) and Unity Student tutorials... only brief Pascal.

avatar image KellyThomas · Dec 15, 2013 at 01:18 AM 0
Share

The three concepts being touched on are:

  • Class

  • Instance *start reading here, it has a summary of the Class page

  • Class variable also known as a "static member variable"

I'm linking to wikipedia because I don't know any "good getting started in OO" tutorials. If these articles a bit too dry (as wikipedia can be), pickup a beginner C# book from your library. The same OO principles apply with very $$anonymous$$or changes between most languages. As a bonus a c# book will $$anonymous$$ch some of the .NET libraries.

avatar image Darksider · Dec 15, 2013 at 08:04 AM 0
Share

Found this site http://math.hws.edu/eck/cs124/javanotes6/contents-with-subsections.html Should I read all contents until chapter 5?

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

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

Related Questions

Problem with Simple Script - Rotation and Data Types 1 Answer

expecting EOF, found 'else'. 1 Answer

Having trouble with wheel colliders. (IMMA NEWB) 0 Answers

I cannot get the blank, gray editable terrain to show up 0 Answers

Respawner not respawning after object is destroyed 1 Answer


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