- Home /
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?
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.
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.
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.
$$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.
The three concepts being touched on are:
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.
Found this site http://math.hws.edu/eck/cs124/javanotes6/contents-with-subsections.html Should I read all contents until chapter 5?