Randomly assign sprites to an array of GameObjects

I’ve been looking everywhere and every answer to video I check out doesn’t fulfill the needs of my issue or isn’t related at all.

Basically I’ve got an array of GameObjects called dirt, I’ve got 2 public sprites s1 and s2 and I’ve placed all of the required objects inside of the array with the use of dirt = GameObject.FindGameObjectsWithTag("Dirt1");.

My issue is that I can’t even get 1 of the arrayed objects to change their sprite with a direct relation let alone getting them all to change to a random sprite.

This is my code so far:

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

public class SpriteSwap : MonoBehaviour {

	GameObject[] dirt;

	public Sprite s1;
	public Sprite s2;

	float rand;

	SpriteRenderer sprites;
	// Use this for initialization
	void Start () {
		sprites = GetComponent<SpriteRenderer> ();

		dirt = GameObject.FindGameObjectsWithTag ("Dirt1");

		for (int i = 0; i < dirt.Length; i++) {
		dirt [1].GetComponent<SpriteRenderer> ().sprite = s2;
	}


	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

using the current for loop, it just somehow manages to clear the sprite renderer of any sprites. The public Sprites are assigned in the Inspector and I tried utilizing sprites but that just leads to an error.
I am completely 100% stuck on this on.

If someone could at the very least help me to get at least 1 singular object to change it’s sprite because from there I should be able to get the rest to change randomly on my own with some help from Google.

Thanks in advance.

Assuming that dirt = GameObject.FindGameObjectsWithTag ("Dirt1"); is actually returning anything, and that your dirt instance actually has a SpriteRenderer component (it would be helpful to post the error you’re getting), the issue is probably in your for loop, which is not using the iterator value, and just using the 1 index value, which is potentially out of bounds.

try swapping dirt [1].GetComponent<SpriteRenderer> ().sprite = s2;
for dirt =*.GetComponent<SpriteRenderer> ().sprite = s2;*
to ensure the loop does not go out of bounds.