Make Instance of a script

I am new to Unity and programming and I have a question.

I have a scriptA that is shooting my object and when this is detected it decreses the life of the object. The life counter is inside ScriptB.

In script A I wrote ScriptB.Life–; (when the object was hit);

This is working but the problem is that every object in the level, and there will be alot of them, have their life decresed by one.

Is there a way to somehow make a “copy of the script” for every object spwaned into the game and access that variable?

ANy suggestions would be great =)

Well, instead of using static variables as you seem to be doing, why not do it in the usual, object-oriented way?

Make ScriptB a component that gets attached to each ‘damageable’ object in the scene. In your hit algorithm, just use

hitObject.GetComponent(ScriptB).life -= 1;

Then, it only decreases the life of one object, not all of them!

I figured it out.

Here is my error.

I thought that Unity, when creating my objects, would attach a copy of ScriptB to it which it does!!

I then thought I could change the variable from ScriptA using
Getcomponent(ScriptB).life–;
Which you can!

Problem is I was making the life variable static which create only one life variable which can be seen by all scripts and thus when I change it all objects are affected it.

Solution- Use getCompoent but do not make my life variable a static.

AM I correct?