Search Unity

Question Working Unity scoreboard system?

Discussion in 'VR' started by radnovaxwavez, Nov 2, 2022.

  1. radnovaxwavez

    radnovaxwavez

    Joined:
    Jun 6, 2021
    Posts:
    1
    Hi All,

    I'm trying to create a simple scoreboard for a VR tennis game i'm making using unity and the XR Interaction Toolkit, The scoreboard is supposed to just go up by 1 whenever the racket hits the ball (These are thrown automatically). Every approach I've taken has either thrown an error or hasn't changed the number on the scoreboard.

    What I have right now is basically:

    The tennis racket script:
    Code (CSharp):
    1. public class TennisRacket : MonoBehaviour
    2. {
    3.     //Scoreboard scoreBoard;
    4.     public GameObject scoreText;
    5.     public int theScore;
    6.  
    7.     UI_Manager _uIManager;
    8.  
    9.     void Awake()
    10.     {
    11.  
    12.     }
    13.  
    14.     private void OnCollisionEnter(Collision other)
    15.     {
    16.         if (other.collider.tag == "Ball")
    17.         {
    18.             _uIManager.AddScore(5);
    19.         }
    20.     }
    21. }
    22.  
    Scoreboard script held on a separate UI_Manager gameobject:
    Code (CSharp):
    1. //Variable for Score Text
    2.     [SerializeField]
    3.     private Text _scoreText;
    4.  
    5.     //Variable for Score Number
    6.     [SerializeField]
    7.     private int _score;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         //Score set to 0 when started
    13.         _scoreText.text = "S" + 0;
    14.     }
    15.  
    16.     public void AddScore(int _killPoint)
    17.     {
    18.         //Add EnemyKillPoint to the score
    19.         _score += _killPoint;
    20.         //Update ScoreText to match the score
    21.         _scoreText.text = "Score " + _score.ToString();
    22.     }
    I've tried so many variations of this to no avail, as far as I can tell it's something to do with the colliders but I can't be sure. The one above throws no errors but doesn't affect the _score int at all.

    Has anyone else tried something similar?
    Thanks in advance.