Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do you lose points on collision?

Discussion in 'Scripting' started by Mit_Goswami17, Aug 19, 2021.

  1. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    I am a beginner game dev and I'm making a game where if the player collides with a wall they lose 10 points. I have gotten everything else to work. including the points system and movement. Now the only thing left is the actual challenge. The script for the point system is:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    public class Timer : MonoBehaviour
    {
    public Transform player;
    bool stopwatchActive = true;
    float currentTime;
    public Text currentTimeText;
    int score;
    public Text scoreText;
    public Text highScore;
    public float multiplayer = 5;
    // Start is called before the first frame update
    void Start()
    {
    currentTime = 0;
    highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    }
    // Update is called once per frame
    void Update()
    {
    if (stopwatchActive == true)
    {
    currentTime = currentTime + Time.deltaTime;
    }
    score = Mathf.RoundToInt(currentTime * multiplayer);
    scoreText.text = score.ToString();
    TimeSpan time = TimeSpan.FromSeconds(currentTime);
    currentTimeText.text = time.ToString(@"mm\:ss\:ff");
    if
    }
    public void HS()
    {
    if (score > PlayerPrefs.GetInt("HighScore", 0))
    {
    PlayerPrefs.SetInt("HighScore", score);
    highScore.text = score.ToString();
    }
    }
    }
     
  2. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Hi. To make it easier for everyone to help you, put your code in a special field, that you can access by clicking the first icon to the right of the "Code:" in the edit bar of the editing field.

    Now to your question. While trying to create any sort of system, try dividing your desired system into several "blocks". For your issue that would be:
    • detect a collision occuring.
    • subtract an int of 10 from whatever point counter variable that you have when a collision is detected.
    The way you can detect a collision is by using Collider.OnCollisionEnter(). More info on that is here: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html.

    Collider collision detection is also one of the most popular topics that come up in Unity Tutorials on various platforms so take advantage of that.
     
    Mit_Goswami17 likes this.
  3. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6.  
    7. public class Timer : MonoBehaviour
    8. {
    9.     bool stopwatchActive = true;
    10.     float currentTime;
    11.     public Text currentTimeText;
    12.  
    13.     int Loss = 10;
    14.     int score;
    15.     public Text scoreText;
    16.     public float multiplayer = 5;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         currentTime = 0;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (stopwatchActive == true)
    28.         {
    29.             currentTime = currentTime + Time.deltaTime;
    30.         }
    31.         score = Mathf.RoundToInt(currentTime * multiplayer);
    32.         scoreText.text = score.ToString();
    33.         TimeSpan time = TimeSpan.FromSeconds(currentTime);
    34.         currentTimeText.text = time.ToString(@"mm\:ss\:ff");
    35.     }
    36.  
    37.     void OnCollisionEnter(UnityEngine.Collision collisionInfo)
    38.     {
    39.         if (collisionInfo.gameObject.name == "Obstacle")
    40.         {
    41.             score -- Loss;
    42.         }
    43.     }
    44. }
     
  4. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    Thank you. I found out how to solve my problem, but now Unity says I'm missing a semicolon. (Error code 1002) but I quadruple-checked the code and I see no problem. I gave Loss a variable of 10 and made a collision if statement.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    The computer is always right. This however, cannot possibly be right:

    Here is how to actually read errors all by yourself:

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  6. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    I check the error codes on the Microsoft website which lists the problems and how to fix them. cs1002 means I am missing a semicolon.
    I set Loss as a variable for 10.
    and score as the current ammount of points the player has.
    I don't know how to write collision statements so i might be using a playerController where i need a rigidbody or something... I'll replace Loss with 10 and put it between parenthesis. But now it gave me 2 errors and 1 warning
    saying CollisionInfo is not in context as well as one about Delegates?
    Code (CSharp):
    1.  
    2. // errors are in this section
    3. void OnCollisionEnter(UnityEngine.Collision collisionInfo)
    4.     {
    5.         if (collisionInfo.gameObject.name == "Obstacle")
    6.         {
    7.             if (CollisionInfo.gameObject.tag == "Obstacle")
    8.             {
    9.                 (score) -- (10);
    10.             }
    11.         }
    12.     }
     
  7. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    I know I'm definitely doing something wrong
     
  8. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    I haven't read the whole thread but this line jumped in my eyes.
    score --;
    would reduce score by 1.
    score -= 10;
    would reduce it by 10.
     
    Mit_Goswami17 likes this.
  9. Mit_Goswami17

    Mit_Goswami17

    Joined:
    Jun 2, 2021
    Posts:
    6
    Thankyou!!! this fixed everything