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);
}
}
}
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