How to add variables to a list?

Hi guys! I’m new to c # and currently have a question, please help !;
The question is the following: how can I add a variable or property? (I don’t know what it is called) to an array or list of GameObject?
I have to make a list of objects (GameObject), since I am not sure how many objects I have to add in the list, I want all the objects that are inside the list to be modified, without having to be placing object [0]. GetComponent, object [ 1] .GetComponent, object [2] .GetComponent, and so on.
I hope you give me to understand.
For example, I want to access the component of a GameObject, it has a script and within it there are two properties, I want all the components of the objects in the list to be “modified” in “General”.
This is what i could do:

public class ObjectList : MonoBehaviour
{
public bool targetHit;
    public List<GameObject> hitOb = new List<GameObject>();
void update(){
if (targetHit == true)
        {
                gameObject.GetComponent<HitMe>().YouHitMe();
        }
if (targetHit == false)
        {
                gameObject.GetComponent<HitMe>().YouNotHitMe();
        }
}
}

I know it must be wrong.
YouNotHitMe and YouHitMe are the two properties of the script that I want to render.
I do not speak English much, I am using Google Translator, I hope it is understood.

You can use this to add an object to your List:

hitOb.Add(GameObject);

and then if you want to do something to every value inside of the list, you can loop over it like this:

foreach (GameObject obj in hitOb)
{
    if (targetHit)
        obj.GetComponent<HitMe>().YouHitMe();
    else
        obj.GetComponent<HitMe>().YouNotHitMe();
}

First, update() is wrong. It must be Update() if you want Unity to call it.

Second, check the documentation on List() to see how to add and get objects from a list.

How to do tutorials properly:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Here are some great starter tutorials:

Imphenzia / imphenzia - super-basic Unity tutorial:

Jason Weimann:

Brackeys super-basic Unity Tutorial series:

Sebastian Lague Intro to Game Development with Unity and C#:

Imphenzia: How Did I Learn To Make Games:

If you want to access the components on each item in a list of gameObjects, then you need to loop through that list and access it just like you said, with an index number. Or, as @RadRedPanda points out, a foreach loop. Either way, you need to loop through it.

Good stuff @Kurt-Dekker , although I don’t think these tutorials will help as much for someone who doesn’t understand English :stuck_out_tongue:

Unfortunately, I don’t know of any good Unity tutorials in other languages, so it might be hard to link those.

¡¡Thank you!!@RadRedPanda