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
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
Related articles
- c # - i want to replace the texture of unity material at a specified time
- c # - rotation control of 3d game objects in unity
- c # - unity about camera control i want to zoom with mouse scroll
- unity - control of movement by rigidbody
- c # - i want one script to control multiple objects
- c # - unity) show/hide prefab button conditionally
- c # - unity) video thumbnails
- c # - about unity ads test ad display
- c # - unity 202010f1 + oculusrift hmd position tracking is not reflected in the game
- about programming basics unity c #
- c # - [unity ray cast] execute the function of the object that received the ray
- c # - [unity 2d] i want to create a gimmick that clings to a rope, hangs, swings like a pendulum, and jumps
- unity - control transform with coroutine
- unity c # error code indexoutofrangeexception: index was outside the bounds of the array
- c# - when using urp of unity, the material color of the object far from the camera becomes light
- c # - about player prefs for unity
- [unity c #] how to completely stop walking when switching panels
- how to use tolist (): and linq;in unity c #
- [c # unity] how to set arrey or dictionary in scriptableobject?
- c # - when controlling ui in unity, there are times when you can access an instance of an object and times when you cannot
Related questions
- c # - [unity] i am changing the value of a public variable from another script, but the value seen from the script with that pub
- c # - play sound effects using unity is trigger
- c # - large number of same errors (using vuforia)
- c # - unity3d: rigid body speed limit
- c # - i want to run unity: transformtranslate using rigidbody
- c # - error "nullreferenceexception: object reference not set to an instance of an object"
- c # - create a new scene with a script
- c # - i made a unity oculus rift laser pointer, but it doesn't respond
- c # - compile error when importing oculus integration in unity 20194
- c # - vroid in unity
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];