How do you lose points on collision?

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

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: Unity - Scripting API: Collider.OnCollisionEnter(Collision).

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.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Timer : MonoBehaviour
{
    bool stopwatchActive = true;
    float currentTime;
    public Text currentTimeText;

    int Loss = 10;
    int score;
    public Text scoreText;
    public float multiplayer = 5;

    // Start is called before the first frame update
    void Start()
    {
        currentTime = 0;
    }

    // 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");
    }

    void OnCollisionEnter(UnityEngine.Collision collisionInfo)
    {
        if (collisionInfo.gameObject.name == "Obstacle")
        {
            score -- Loss;
        }
    }
}

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.

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.

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?

// errors are in this section
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
    {
        if (collisionInfo.gameObject.name == "Obstacle")
        {
            if (CollisionInfo.gameObject.tag == "Obstacle")
            {
                (score) -- (10);
            }
        }
    }

I know I’m definitely doing something wrong

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.

1 Like

Thankyou!!! this fixed everything