修復我試圖訪問另一個腳本中包含的類,我得到這個:「Internal_Create只能從主線程調用」。 .init函數只是改變類中的一些值。Unity3d:通過GetComponent錯誤從另一個腳本訪問類
我試圖通過搜索答案來解決它,但我找不到任何有用的東西。
這是發生錯誤的主循環:
public class MainLoop : MonoBehaviour {
public float jagginess;
void Start() {
jagginess = 0.6f;
CMesh cmesh = GameObject.Find("GameObject").GetComponent<CMesh>();
cmesh.init(32);
}
void Update() {
//if (Input.GetKeyDown(KeyCode.Space)) {
}
}和網格類:
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class CMesh : MonoBehaviour {
Mesh mesh = new Mesh();
Texture2D heightMap;
int segments;
Vector3 scaleSegments;
Vector3[] vertices;
Vector3[] normals;
Vector2[] uv;
int[] triangles;
Vector3 camera_position = GameObject.Find ("Main Camera").GetComponent<cameraMovement>().camera_pos;
public void init(int mesh_segments) {
scaleSegments = new Vector3(200, 100, 200);
segments = mesh_segments;
initHeightMap();
}
@Burdock '使用UnityEngine;使用System.Collections的 ; public class MainLoop:MonoBehaviour { \t \t public float jagginess = 0.6f; \t \t \t空隙開始(){ \t \t \t \t CMesh cmesh =(CMesh)gameObject.GetComponent(); \t \t cmesh.init(32); \t \t \t } \t \t空隙更新(){ \t \t \t // \t如果(Input.GetKeyDown(密鑰號碼。空間)){ \t \t} }' –
whoadrian
在評論中發佈大量代碼並不是一個好主意。你可以隨時編輯你的文章。這就是說,你的班級沒問題。看看我的答案,它應該解決你的問題。 – 2013-12-21 00:59:41
'使用UnityEngine;使用System.Collections的 ; [RequireComponent(typeof運算(MeshFilter))] [RequireComponent(typeof運算(MeshRenderer))] [RequireComponent(typeof運算(MeshCollider))] 公共類CMesh:MonoBehaviour { \t \t網目=新目() ; \t \t \t \t Texture2D heightMap; \t \t \t \t int segments; \t \t的Vector3 scaleSegments =新的Vector3(200,100,200); \t \t \t \t \t \t \t 的Vector3 \t = camera_position GameObject.Find( 「主相機」)GetComponent()camera_pos。; \t \t \t公共無效的init(INT mesh_segments){ \t \t \t \t \t \t段= mesh_segments; \t \t \t \t \t initHeightMap(); \t \t} \t \t' –
whoadrian