Unity - Add Material To Game Object Without Setting the Material As A Public Material

I’m am trying to create a script that continuously generates objects and attaches a script to all the generated objects. I want the attached script to after 3 seconds, change the material on the object and add a box collider. My problem is with the material. Since the object was randomly generated, I don’t know how to set the material variable. This is my Spawner:

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

public class Spawner : MonoBehaviour {
    //Variables
    public float x_max;
    public float x_min;
    public float y_max;
    public float y_min;
    private float Size;
    public float s_max;
    public float s_min;
    public float w_max;
    public float w_min;
    private float ProceduralCacheSize;
    private GameObject sphere;
    private float waitTime = 5f;
    private int fix;

    // Use this for initialization
    void Start () {
        Spawn ();
        Spawn ();
        Spawn ();
        StartCoroutine("MyCoroutine");
    }
  
    // Update is called once per frame
    void Update () {
      
    }
    void Spawn(){
            Size = Random.Range (s_min, s_max);
            sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
            sphere.transform.position = new Vector3 (Random.Range (x_min, x_max), Random.Range (y_min, y_max), 0);
            sphere.transform.localScale = new Vector3 (Size, Size, Size);
            var fbs = sphere.AddComponent<Astroid> ();
        }

    IEnumerator MyCoroutine(){
        StartCoroutine("Change");
        waitTime = Random.Range (w_min, w_max);
        yield return new WaitForSeconds(waitTime);
        StartCoroutine("MyCoroutine");
        Spawn ();
    }
}

And the script that gets attached:

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

public class Astroid : MonoBehaviour {
    //I need to be able to set this variable to a material
    public Material mat;
    // Use this for initialization
    void Start () {
        this.GetComponent<SphereCollider>().enabled = false;
        StartCoroutine("Change");
    }
    // Update is called once per frame
    void Update () {
      
    }
    IEnumerator Change(){
        yield return new WaitForSeconds (3);
        this.GetComponent<SphereCollider>().enabled = true;
        this.GetComponent<Renderer> ().material = mat;
    }
}

How do you expect the “StartCoroutine(“Change”);” in your “MyCoroutine” to be able to see the “Change()” function on the Asteroid script? You’re not referencing which Astroid script to call it on.

Change “var fbs = sphere.AddComponent ();” to “Astroid fbs = sphere.AddComponent ();”

Then you can right after that do:

fbs.mat = whateverMaterial.
And then manually call the “Change()” function on the script.

Thought you’ll want to look into Object Pooling, as constantly spawning and destroying objects will create lots of garbage and lag your game.

The spawner calling the Change Coroutine was a typo. Sorry. call Change is the Astroid script. But the problem still persists.

I described how to do it, what issue are you having following what I mentioned?