0
votes

Can someone please tell me why my gameobject score value isn't increasing on my screen during game play but is within my inspector. I am creating a hidden object game and every time you click on a object the score will gradually go up. The first script I am showing is the one which is attached to all the object and is picking up on the clicks... clickobj

using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;

public class clickobj : MonoBehaviour, IPointerClickHandler
{

    Score score;

    void Start()
    {
        addPhysics2DRaycaster();
        //Get Score Script Instance
        string scoreObject = "office";
        score = GameObject.Find(scoreObject).GetComponent<Score>();
    }
    void addPhysics2DRaycaster()
    {
        Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        //Click detected. Increment score
        score.score++;
        Destroy (gameObject);
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);

    }

}

The next script is the Score.cs script which I have attached to the scene and I have also drop the score object within the script as see below on the screenshot. Score.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour {


    public int gameObject;

    public int score;
    public Text scoreText;
    // Use this for initialization

    void Start () {
        score = 0;
        UpdateScore ();
    }
    void OnMouseDown () {
        score += gameObject;
        UpdateScore ();
    }
    // Update is called once per frame
    void UpdateScore () {
        scoreText.text = "Score:\n" + score;

    }
}

enter image description here

1

1 Answers

1
votes

Change your OnPointerClick to

public void OnPointerClick(PointerEventData eventData)
{
    //Click detected. Increment score
    score.score++;
    score.scoreText.text = "Score:\n" + score.score;
    Destroy (gameObject);
    Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);

}