- Home /
Keeping score when object clicked and destroyed
I'm having problems keeping score when destroying objects when they are clicked. I think its because of the clones I'm generating. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Touch : MonoBehaviour {
public GameObject pinnedRing;
RaycastHit2D hitInfo;
public int score;
void Start()
{
score = 0;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
//Debug.Log ("Clicked");
Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
if(hitInfo.collider.tag == "Ring")
{
score += 1;
Destroy (hitInfo.collider.gameObject);
Vector3 pinPos = hitInfo.transform.position;
GameObject secondRing = Instantiate (pinnedRing, pinPos, transform.rotation);
Destroy(secondRing, 1f);
}
if(hitInfo.collider.tag != "Ring")
{
//Do nothing
}
}
Debug.Log (score);
}
}
Basically, I spawn the objects and when I click them they disappear, but the score doesn't add, it just keeps on 0. How can I fix this?
According to this code, your score only goes up if the first ring is destroyed, not the second one. Is that what you want?
@sean244 I want the score to go up every time a ring is destroyed
I just noticed that when I destroy a ring it gives the error "Object reference not set to an instance of an object", sending me to line 18 (if(hitInfo.collider.tag == "Ring"))
Answer by Harinezumi · Apr 17, 2018 at 10:41 AM
You need to check if the raycast hit anything at all, because if not, then hitInfo.collider will be null, and so accessing hitInfo.collider.tag will give the error you described in the comment.
In code:
RaycastHit2D hitInfo = ...;
if (hitInfo.collider == null) { /* do nothing */ }
else {
// do the same as you did originally
}
Answer by GMAGames · Apr 17, 2018 at 11:16 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Touch : MonoBehaviour {
public GameObject pinnedRing;
private int score;
Vector3 pinPos;
void Start()
{
score = 0;
}
void Update() {
RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward, Mathf.Infinity);
Debug.DrawRay (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward, Color.red);
if (hitInfo.collider != null) {
if (hitInfo.collider.tag == "Finish") {
score += 1;
pinPos = hitInfo.transform.position;
Destroy (hitInfo.collider.gameObject);
Invoke("Spawn",1f);
}
Debug.Log ("done");
if (score > 0)
Debug.Log (score);
}
}
void Spawn()
{
GameObject secondRing = Instantiate (pinnedRing, pinPos, Quaternion.identity);
}
}
you must create a prefab of the ring object and tag = Finish
Your answer
Follow this Question
Related Questions
How to keep score? 1 Answer
Destroyed clones freezing but not disappearing? 1 Answer
Ignoring collisions with clones 3 Answers
How do I destroy a specific clone of a GameObject? 1 Answer
4 blocks destroyed - gravity true 2 Answers