- Home /
На вопрос ответили. Правильный ответ был принят
i can't implement the scoring system c#
hi i am new to c #, i would like to implement this: there is a script that clones the q object at random points when pressed. I wish that every time I click on the q object, +1 point is added and the score is recorded on the screen. thanks in advance
script added below
object spawn:
public GameObject qPrefab;
public Vector3 center;
public Vector3 size;
internal bool lose;
public bool next { get; internal set; }
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null)
{
if (hit.collider)
{
if (hit.collider.tag == "q")
{
if (Input.GetMouseButtonDown(0))
{
SpawnNextq();
}
}
}
}
}
void SpawnNextq()
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2),
Random.Range(-size.y / 2, size.y / 2),
0);
Instantiate(qPrefab, pos, Quaternion.identity);
Destroy(gameObject);
}
scoring:
public void OnClick()
{
score++;
xText.text = score +"";
}
Answer by GDGames0302 · Jan 30, 2021 at 06:40 PM
(This works if you want the score to increment when you spawn a new object) Hi. You should declare an int and set it to 0(public int score = 0;). In the SpawnNextq function increment the score(score ++;). Then to show it on screen, you should include UI at the top(using UnityEngine.UI;). Declare a text(public Text scoreText;). In the SpawnNextq function set the text with the score value(scoreText.text = score.ToString();). Create that text in the project and assign it to the new slot from this script. If you do like so, you will see the score as an int(1, 5,13....). If you want to see the score as Score: 1 or Score: 5, you should do in the SpawnNextq function the following: scoreText.text = "Score: " + score;
(I'm sending this from my phone, that's why there are no spaces)
using UnityEngine; using UnityEngine.UI; public class...... : $$anonymous$$onoBehaviour{
public Text scoreText; public int score;
public GameObject qPrefab; public Vector3 center; public Vector3 size; internal bool lose; public bool next { get; internal set; } void Update() { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y); RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero); if (hit.collider != null) { if (hit.collider) { if (hit.collider.tag == "q") { if (Input.Get$$anonymous$$ouseButtonDown(0)) { SpawnNextq(); } } } } } void SpawnNextq() { Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), 0); Instantiate(qPrefab, pos, Quaternion.identity); Destroy(gameObject); }
public void OnClick() { score++; scoreText.text = "Score: " + score; }
}
Answer by CmdrZin · Jan 30, 2021 at 06:35 PM
What you want is fairly simple, but does take a fare amount of code: A UI, Colliders, Events, etc. Try working though some tutorials like https://learn.unity.com/project/roll-a-ball
Follow this Question
Related Questions
Rigidbody2D.attachedColliderCount question C# 0 Answers
New score each level 1 Answer
Scoring Points. 1 Answer
Score Count Bug 3 Answers