- Home /
Points-Counter Problem
Hello guys. So I have in my scene 3 canvas. Each canvas has 3 buttons. Each button can give you 0 points, x3 points, x5 points and x10. When you click an object in the scene the canvas gets enabled.Here is the script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonOnClickActive : MonoBehaviour {
public GameObject canvas;
private bool clicked;
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0) && !clicked)
{
canvas.SetActive(true);
clicked = true ;
GetComponent<Renderer>().material.color = Color.red;
}
}
}
And here is the script for points it is the same for each one just with changed numbers(x3,x5,x10)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class x3 : MonoBehaviour {
public Text scoreText;
public int point;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
scoreText.text = point + " x3";
if (Input.GetMouseButtonDown(0))
{
point++;
}
}
}
Every button has a script accordingly to what points it is going to give. The thing is that when you click the objects, points are added automaticaly adn when you click a button more points are added for every button. Can anyone help me out? Cheers
Answer by I5 · Sep 11, 2018 at 04:46 PM
Ah, I think I understand. This isn't fully debugged and/or tested, but I think this will get you there. Add this class/behavior to one, and only one, "SceneManager" gameObject that's always active and enabled (can be an empty game object).
//add this class/component to one, and only one, "master" game object that's always active in the scene, can be an empty game object
public class SceneManagerClass : MonoBehaviour {
public Camera aCamera;
[Header("This links game objects in scene to canvases/UI's to be activated")]
public GameObjectCanvasLink[] goToCanvasLinks;
void Start() {
foreach (GameObjectCanvasLink goCLink in goToCanvasLinks) {
Button[] buttons = goCLink.c.gameObject.GetComponentsInChildren<Button>(true);//assuming that all buttons in the canvas give points
for (int i = 0; i < buttons.Length; i++) {
Button b = buttons[i];
b.onClick.AddListener(delegate { addPoints(b); });
}
}
}
public void addPoints(Button b) {
//add the points here based on the name of the button
int points = int.Parse(b.gameObject.name.Substring(0, b.gameObject.name.IndexOf('_')));//ex button name "10_MyButton"
//do something with the points
if (lastActiveCanvas != null) lastActiveCanvas.gameObject.SetActive(false);
}
private Ray ray;
private Canvas lastActiveCanvas = null;
void Update() {
if (lastActiveCanvas != null && lastActiveCanvas.gameObject.activeInHierarchy) return;
if (Input.GetMouseButtonDown(0)) {
ray = aCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray, 100f);//change 100 to how far the ray should soot
for (int i = 0; i < hits.Length; i++) {
foreach (GameObjectCanvasLink goCLink in goToCanvasLinks) {
if (hits[i].collider.gameObject.GetInstanceID() == goCLink.go.GetInstanceID()) {
goCLink.c.gameObject.SetActive(true);
lastActiveCanvas = goCLink.c;
break;
}
}
if (lastActiveCanvas != null && lastActiveCanvas.gameObject.activeInHierarchy) break;
}
}
}
[Serializable]
public struct GameObjectCanvasLink {
public GameObject go;
public Canvas c;
public GameObjectCanvasLink(GameObject _go, Canvas _c) {
go = _go;
c = _c;
}
}
}
Answer by stylianos-theodorikakos · Sep 11, 2018 at 07:10 PM
thank you for your reply, the thing is to use RaycastAll is a but intermediate to understand and im a beginner. So i tryed to add a point every time the canvas is enabled. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour {
public Text scoreText;
public int point;
void Update () {
scoreText.text = point + " x3";
if (gameObject.activeSelf){
point++;
}
}
}
It works good but it doesnt stop unless the canvas gets disabled again. All I need is to add only 1 point. Any thoughts on how to take this parameter?
I don't understand. Why are you adding a point and changing the text every frame in Update, without regard to a mouse click? Can you restate what you're trying to do, maybe with screenshots?
ok, i think i understand. I updated/edit my original answer with new code. Not fully debugged, tested and/or architected, but I think it'll get you there.
Your answer
Follow this Question
Related Questions
Non stopping point - counter 1 Answer
The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers
HELP! Scripts looks fine but doesn't respond? 1 Answer
UnityEngine.UI.dll 0 Answers
Double Score Problem ? 1 Answer