Home>
I want to control infinitely generated Prefab material with time.

In Unity3D, I am creating a script that generates infinite Prefabs.
I want to loop the Prefab material from Material1 to Material2 to Material3 to Material1 at regular intervals.
Infinite generation of Prefab could be performed, but it was clogged at the time control stage of the material.


Error message
Error message
Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateMultiple: MonoBehaviour {
    public Material [] _material;// Material to assign.
    float time = 5;
    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
        for (int i = 0;i<20;i ++) {
                time-= Time.deltaTime;
                if (time<= 0)
        {
                     // Create an instance by passing Prefab as an argument of Instantiate
                     GameObject obj = (GameObject) Resources.Load ("CarPrefab");
                     // place in a random location
                     Instantiate (obj, new Vector3 (Random.Range (250, 4), 5, Random.Range (100, 4)), Quaternion.identity);
                     time = Random.Range (3, 6);
             }
         }
    }
}

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    GameObject car = Instantiate (~);
    The object placed in
    car.GetComponent(). material = _material [0];
    You can change to the specified material with.

    After that, change the_material [0]part appropriately.

    If you can switch at the same frequency as creation, create one int variable in the class.
    int materialIndex;
    Add (return to 0 when it reaches the end) every time it loops
    if (materialIndex == _material.length-1) {materialIndex = 0;} else {materialIndex ++;}
    Can it be applied to objects?
    car.GetComponent(). material = _material [materialIndex];