Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 01, 2021 at 06:40 PM by artartem117 for the following reason:

На вопрос ответили. Правильный ответ был принят

avatar image
0
Question by artartem117 · Jan 30, 2021 at 06:00 PM · score systemcounting

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 +"";
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GDGames0302 · Jan 30, 2021 at 07:07 PM 1
Share

(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; }

}

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D.attachedColliderCount question C# 0 Answers

New score each level 1 Answer

I'm having trouble reading and writing a text file. I am suppose to have it to where high scores and players initials are saved and displayed in my game. 0 Answers

Scoring Points. 1 Answer

Score Count Bug 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges