1

I know there is a lot of questions about unity scores etc however I am finding it hard to actually structure the code for my game and have reviewed loads of youtube tutorials and blogs posts. I was wondering if anyone could help me add a score so that every time I click one of my objects within my hidden object game it provides a score. I would really appreciate it. So far I have added a separate script called Score.cs..The code is as follows below. I then added it to my background image and included the script into each object my Score Text is within the canvas and currently does not count any objects that are being clicked.

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

public class Score : MonoBehaviour {

    public Text scoreText;
    public int gameObject;

    private int score;
    // 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;

    }
}

!***** EDIT (Score.cs) *****!

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

public class Score : MonoBehaviour {

    public Text scoreText;
    public int gameObject;

    public int score;
    // 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;

    }
}

!****** Clicker.cs ******!

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

public class Clicker : MonoBehaviour, IPointerClickHandler
 {

    Score score;

    void Start()
    {
        addPhysics2DRaycaster();
        //Get Score Script Instance
        string scoreObject = "gameObject";
        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++;
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);

    }

}
Rochelle
  • 37
  • 1
  • 8
  • *I click one of my objects within my hidden object game it provides a score."* What do you mean by *"provides a score"*? – Programmer Apr 23 '17 at 21:42
  • @programmer, Sorry I should of described it a little bit better, I mean when the player clicks on each object the score will gradually go up each time – Rochelle Apr 23 '17 at 21:44

1 Answers1

0

It's really, easy. Create a script that detects click on your GameObjects. You can use the OnPointerClick for that. Get instance of the Score script from that script then increment the score variable when OnPointerClick is called.

Make sure to change private int score; to public int score;.

Attach the script to every Object you want to detect click on.

public class Clicker : MonoBehaviour, IPointerClickHandler
{
    Score score;

    void Start()
    {
        //Get Score Script Instance
        string scoreObject = "GameObjectScoreIsAttachedTo";
        score = GameObject.Find(scoreObject).GetComponent<Score>();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        //Click detected. Increment score
        score.score++;

    }
}

If your Object is 2D, you will need to add the addPhysics2DRaycaster() function. If it is 3D you will need to add the addPhysicsRaycaster() function. See here for more information.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you so much for getting back to me, I just tried and still cant manage to gradually update the score once the user clicks on the score. I am receiving the error "NullReferenceException: Object reference not set to an instance of an object Clicker.Start () (at Assets/Clicker.cs:16)" – Rochelle Apr 24 '17 at 10:18
  • *"GameObjectScoreIsAttachedTo"* is supposed to be the name of GameObject the `Score ` from your question is attached to..... Replace that with the name of that GameObject. – Programmer Apr 24 '17 at 10:20
  • I have just tried that and still appear to receive the same message message even though I am using the name of the GameObject. Would you mind if I sent over my game for you to have a look at, I would greatly appreciate it – Rochelle Apr 24 '17 at 10:36
  • I am not on my testing computer now and can't do that now. Update your question with a screenshot of that GameObject the `Score` script is attached to. Also, Add Edit** in your edited question and add the code from my answer. The exact one you are using. Maybe I can spot the problem. – Programmer Apr 24 '17 at 10:40
  • @Rochelle The GameObject with the `Score` script is named *"office"* not *"gameObject"*. So `string scoreObject = "gameObject";` should be `string scoreObject = "office";` – Programmer Apr 24 '17 at 12:35
  • Ahh I see, sorry thank you for telling me that, I just changed it there now but the score still manages to stay the same when I click each object. – Rochelle Apr 24 '17 at 13:26
  • I think that Edge and Polygon Collider2D are not supported. It looks like you are using `Polygon Collider2D` from the screenshot. It has to be Box Collider2D,Circle Collider2D or Capsule Collider2D. Also, don't forget to call the `addPhysics2DRaycaster()` function since you are using SpriteRenderer and 2D Collider. This should work if you follow direction. – Programmer Apr 24 '17 at 13:34
  • @Rochelle Hey, I can still help you. I saw you deleted your comment. Tell me when you finish one of those project tutorials I talked about. I want the help to worth it. – Programmer Apr 29 '17 at 10:51