adding points to game

I want to add points to my game each time I click a cube(I have the script attached to 3 cubes),but when i try printing the score it always shows 0. Tell me what is wrong with this script.

using UnityEngine;
using System.Collections;

public class checkifclick : MonoBehaviour {

    public int points = 0;

    void OnMouseDown () {
        Destroy(gameObject);
        points = points + 1;
    }

    void Update() {
        Debug.Log (points);
    }

}

Since your script selfdestroys you will lose the content of points. If you make it static it will probably work the way you want. Also using OnMouseDown is probably not optimal. Try something like this:

using UnityEngine;
using System.Collections;
public class checkifclick : MonoBehaviour {
    public static int points = 0;
    void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            points = points + 1;
            Debug.Log (points);

            Destroy(gameObject);
        }
    }
}

ok but i need the cube to disappear

If the script is on the cube, it will disappear (note the; Destroy(gameObject)).

ok but this also has another problem,it will add a point if you click anywhere

and it destroys my 3 cubes all at a time

Darn, I didn’t read your post properly. I guess it’s too early in the morning :slight_smile: By all means, do use OnMouseDown, but use static. So:

using UnityEngine;
using System.Collections;
public class checkifclick : MonoBehaviour {
    public static int points = 0;
    void OnMouseDown () {
        Destroy(gameObject);
        points = points + 1;
    }
    void Update() {
        Debug.Log (points);
    }
}

ok thx a lot i am a begginer in this so sorry for putting such a simple question

ok,now i have another question,how can i get to display the points variable in the screen

You should really take your time and look into some tutorials: https://unity3d.com/learn/tutorials/projects/roll-a-ball

allso,i saw a tutorial of how to create text on screen well but it must have been from an old version.Whats wrong with this code?

using System.Collections;

public class GUItext1test : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        guiText.text = "hi";
    }
}

Unity should be telling you, what is wrong with your code. I guess, Unity is telling you, that he cannot access guiText, because you did not declare it anyway. there should be something like

using System.Collections;
public class GUItext1test : MonoBehaviour {
    public Text guiText  ;
    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
        guiText.text = "hi";
    }
}

And then you can assign your gameObject with the text to the field in the inspector of this script…
But again, don’t think, everyone will write down everything for you, as you do not seem to understand the code at all…
This is the last time I advice you to read tutorials, watch videos etc.
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text