2
我正在嘗試創建一個腳本,它不斷生成對象並將腳本附加到所有生成的對象。我希望附加的腳本在3秒後更改對象上的材質並添加框對撞機。我的問題在於材料。由於對象是隨機生成的,我不知道如何設置材質變量。這是我的產卵:Unity - 將材質添加到遊戲對象
3210這是獲取連接的腳本:
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;
}
}
這個答案是不正確。根據[Unity文檔](https://docs.unity3d.com/ScriptReference/Resources.Load.html),擴展名必須省略。 –