Copy a damage value on collision

Imagine a simple scene with a sphere (player) and two cube (A and B). Each cube has a property “damage” like this: Cube A damage = 3 and Cube B damage = 7.
When the sphere collide with one of the cubes, I want it to copy the value of the Cube’s damage property, like if it collides with Cube B the sphere copy the value “7” to its “damageReceiver” property.
But I would like to do that in a clever way so that I can have a scene with a hundred of cubes with their own damage value, and each time the sphere collides with one of them, it copies the damage value of the cube it collided with.

Hi @Corentin_Fousset-Martial

Here’s one solution - You just need to create some MonoBehaviour component, which you attach to any and all of your items that should give damage to your player.

To be honest, I’m not quite sure what you mean by “it copies the damage value of the cube it collided with” but I assume you mean that damage causing object’s value is used to cause damage to player…

Then you can do something like this in your player’s script:

float myHealth = 100f;

void OnCollisionEnter(Collision collision)
	var damager = collision.gameObject.GetComponent<Damager>();

	if (damager != null)
		myHealth -= damager.damageValue;


You should really go through all the Unity Learn material… this is explained there.

Thank You for this ! I have been through a bunch of those tutorial, too bad I didn’t find the one relating to this subject ^^ They’re good though.