How to add Player health and ability to take damage from a cube?

I need to know how to make my player which is the Unity model called “RollerBall” take damage from a sliding cube which are called “cube” and “pillar” on touch and/or make it end the game with an option to restart.

I’m new to all this stuff and could really use the help.
I appreciate your time if you can help me.
Whatever it is you can help me with, i really need you and again appreciate your time. Im willing to collaborate with anyone to make this a reality and happen if needed.

Thanks, Rockstxr

This isn’t tested at all, it’s just to show you how you can do it. Customize it to your needs

#pragma strict
var health: int;

function Start ()
{
health = 100;
}

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "Pillar")
    {
        health-= 25;
    }
    if(health <= 0)
    {
        Destroy(gameObject);
    }

}

Is impressive how people ask things so generic here. I mean… “How to receive damage?” You tell me, it’s your game with your rules, you should be able to imagine them.

Try to be more specific: “How comunicate between two scripts?” or “How show the damage received on a lifebar?” or “How detect the end of a collision?”

This doubt is a very basic flaw in your coding skill, try start getting better on coding before learning Unity.
Answer:
You need to create your rule system. How much life the player has? How the damage is calculated: by weapon? by enemy? the player has an armor to defense himself? This is game design, and you need to have an ideia about how your game will be first.
Supose that you have an Player with an armor, and an Enemy with an weapon, let’s make the following:

  • Class name → Player

  • Fields (define a value to each field as you want):

  • life → int

  • maxLife → int

  • minLife → int

  • armor → int

  • Class name → Enemy

  • Fields

  • life → int

  • maxLife → int

  • minLife → int

  • weapon → damage

Now you will check how collision detection works. And in the collision method you subtract the life by enemy weapon plus armor.

This way your cube is receiving damage. But what goes when the life is zero? You will need to kill the cube, or do something else.