How would one find more than one of the same object type in other gameObjects?

I feel a little stupid because I’m not sure how to word this question right, so i’ll just give an example in hopes that it makes a little more sense:

For example I have two separate Level Sprite Text and likewise I have two surface effectors, how can I make it so the PlayerController can use them both for it’s boost code?

I assume I need to use FindObjectsOfType<> and add the surface effectors in an Array, but how do I make sure that the PlayerController is selecting the correct object for reference during gameplay?

… Or am making this all wrong and there’s a better way to go about it that I don’t know about cause i’m a noob and a half? I’d love any kind of advise or hint about this.

Thanks in advance, and cheers! :grinning_face_with_smiling_eyes:

Hi,

If you want to find multiple objects of the same type FindObject s OfType<> is the correct method. :slight_smile:

However, objects do not have names. For this reason, you cannot distinguish them just by looking at the objects themselves. You could, for example, check the name of the game object to which they are assigned. If that name is unique, you could find a specific object in your array.

Bear in mind, though, that Find* methods are very slow. And a solution where you have to check for things that might easily change (e.g. strings/names) are usually prone to errors and hard to debug. If you know where your two “Level Sprite Text” objects and your two surface effectors are, [SerializeField] would be a more performant and more reliable solution because you manually assign the objects you want, and Unity does not have to look for anything.

Did this help? :slight_smile:


See also:

2 Likes

Thank you for the quick answer. Before continuing my explanation further, I’d like to apologize as I just noticed that I miss-wrote the name of the object I was looking for. I’ll fix it here so it makes more sense in the context of my question.

What I meant earlier was that I’m looking for two separate Level Sprite Terrains which are the Sprite Shapes with the added surface effector. (I am unsure what was I thinking when I wrote “Sprite Text”… Sorry about that.)

Anyway, here’s the follow up:

The way I’m trying to make it work with multiple surface effectors is by adding them in an array (experimented with both FindObjectsOfType<> and just by adding them in the inspector for learning’s sake, both worked, so yay for that!) and the only way I could figure out about defining the speed value was this way…

srfcEffArr[0].speed = boostSpeed;
srfcEffArr[1].speed = boostSpeed;

…which works, but surely there must be a better way to define the speed variables than writing over every single cell in the array?

Also if I decide to use FindObjectsOfType instead, and there’s less surface effectors in a level, the console will print IndexOutOfRangeException errors. I get why, the code wants to define speed variables of things don’t exist in the current context and that breaks the code.

So I guess I have two questions:

  1. Is there a better way to define speed variables in an array?
  2. If using FindObjectsOfType, how can one define speed variables without getting an out of bounds error in the array?

You could use a loop.

For loop

 for (int i = 0; i < srfcEffArr.length; i++)
        {
            srfcEffArr[i].speed = boostSpeed;
        }

Of foreach loop

foreach (SurfaceEffector2D effector in srfcEffArr)
        {
            effector.speed = boostSpeed;
        }

This will assign your set value in the boostSpeed variable to the speed property of every SurfaceEffector class instance in your array.

1 Like

Oh, I should’ve figured that would work. I’ll try that, thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms