[JS] The numbers do not add up to the var

Welcome. I’m trying to create a script that adds a button is pressed var number 1 I mean the counter, which will be summed up how much we have collected things. Unfortunately, this counter is not working and is not added to 0 numbers. Sorry for bad english, but I am writing this with google translator.

Here is the code:

var tekst : GUIText;
var fpc : GameObject;
var bron : GameObject;
var vis : boolean;
var Hand : GameObject; 
var distance;
var coconut = 0;

static var bronZabrana : boolean = false;


     

function OnBecameVisible() {

vis = true;

}

function OnBecameInvisible () {

vis = false;

}

function Update()
{
distance = Vector3.Distance(bron.transform.position, fpc.transform.position);


if ((distance < 1.1)  (vis == true))
{
tekst.enabled = true;

if(Input.GetKeyDown("f"))
{


bronZabrana = true;
tekst.enabled = false;
gameObject.active = false;
bron.transform.parent = Hand.transform;
	function OnTriggerEnter (other : Collider) {
		coconut++;
	}
}
}

if ((vis == false) || (distance > 1.1))
{
tekst.enabled = false;
}

}
      
    
    
        function OnGUI () {
    GUI.Label (Rect (10, 10, 100, 200),"pozostałe części: " + coconut + " /10");
    }

Can anyone help?

edit: Does your object have a renderer attached to it?

I have got bugs now :confused: BCE0044 BCE0043 and UCE0001

Yes, Mesh Renderer

I don’t think you are making your search in the right script, you have a syntax error in script podnoszenie.js which contents a OnTriggerEnter function which is not present in the script you pasted.

So, what can I do ?

Syntax error is because you did not write the code correctly. Error should take you to the line where there error is and make sure you enter code correctly.

Post the code from podnoszenie.js if you want people to take a look at it.

I do not see any error.

    var tekst : GUIText;
    var fpc : GameObject;
    var bron : GameObject;
    var vis : boolean;
    var Hand : GameObject;
    var distance;
    var coconut = 0;
     
    static var bronZabrana : boolean = false;
     
    function OnBecameVisible() {
             vis = true;
    }
     
    function OnBecameInvisible () {
             vis = false;
    }
     
    function Update()
    {
      distance = Vector3.Distance(bron.transform.position, fpc.transform.position);
      if ((distance < 1.1)  (vis == true))
      {
        tekst.enabled = true;
     
        if(Input.GetKeyDown("f"))
        {
          bronZabrana = true;
          tekst.enabled = false;
          gameObject.active = false;
          bron.transform.parent = Hand.transform;
     
          function OnTriggerEnter (other : Collider) {
            coconut++;
          }
        }
      }
     
    if ((vis == false) || (distance > 1.1))
    {
      tekst.enabled = false;
    }
     
    }
     
    function OnGUI () {
      GUI.Label (Rect (10, 10, 100, 200),"pozostale czesci: " + coconut + " /10");
    }

The following …

function OnTriggerEnter (other : Collider) {
            coconut++;
}

… should not be inside the “function Update()” function. Move it outside, like the other functions are.

Then how do to an added points after picking up item?

I’m not exactly sure what you’re trying to achieve, but perhaps all you need is "coconut++; " at line 32?

You haven’t close the bracket is your If statement line 26
and I’m not sure you can put a function like OnTriggerEnter in your if statement

If you would like to take the object, I suggest you to put the If(Ipnut… in the OnTriggerEnter instead