[英]Unity - Add Material To Game Object
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.
我希望附加的脚本在3秒后,更改对象上的材质并添加一个盒子对撞机。 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:
这是我的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");
StartCoroutine("Change");
Spawn ();
}
}
And this is the script that gets attached: 这是附带的脚本:
using 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;
}
}
You could just load it from resources 您可以从资源中加载它
mat = Resources.Load("myMaterial.mat", typeof(Material)) as Material; mat = Resources.Load(“ myMaterial.mat”,typeof(Material))作为Material;
You can make a public static int like this. 您可以像这样使public static int。 Your Spawner:
您的Spanner:
public Material mat; public static Material mat2; void Start(){ mat2 = mat; }
And in your astroid script: 在您的astroid脚本中:
using 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 = Spawner.mat2; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.