FindObjectWithTags + GetComponents

Hello. I am trying to find all the components with a specific tag and get the component “image”.
Then change the Image color but I ran into an error. Been experimenting but no results.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VärviM : MonoBehaviour
{

    private GameObject[] ImageUI_obkt;
    private Image[] ImageUI_Compo;

    public void värv_Punane()
    {

        ImageUI_obkt = GameObject.FindGameObjecstWithTag("UI_image");
        ImageUI_Compo = ImageUI_obkt.GetComponents<Image>();

    }




}

For future reference I would suggest learning this style of code structure : Performantly handle Script to Script Communication - Questions & Answers - Unity Discussions

As it will help you greatly. But it looks like you are trying to cache those variables(components), so it is best to declare these within Awake() or Start(), so they only “get” once :

void Awake()
{
    ImageUI_obkt = GameObject.FindGameObjecstWithTag("UI_image");
    ImageUI_Compo = ImageUI_obkt.GetComponents<Image>();
}

And to the heart of your issue, you are declaring these with capital letters. If you are not calling a class, void, enum state, or component always start your variables with lower case letters.

And more than likely your error is because you are not iterating through the array. So I would set it up kinda like this :

private GameObject[] imageUI_obkt;
private Image[] imageUI_Compo;

void Awake()
{
	imageUI_obkt = GameObject.FindGameObjecstWithTag("UI_image");
	imageUI_Compo = new imageUI_Compo[imageUI_obkt.Length];

	for (int i = 0; i < imageUI_Compo.Length; i++)
	{
		imageUI_Compo <em>= imageUI_obkt*.GetComponent<Image>();*</em>

* }*
}
I may have missed something, not sure… But give this a try and see if it doesn’t work :)