2014-12-09 3 views
2

유니티 3D에 비해 다소 새로운 (사실 아주 새로운 8 시간 미만) 새입니다.유니티에서 동작을 인스턴스화 할 수 없습니다.

Unity에서의 새로운 점은 다소 이상한 문제로 나를 위태롭게했습니다.

행동 CamCaptureDialogBehavior :

using UnityEngine; 
using System.Collections; 

public class CamCaptureDialogBehavior : MonoBehaviour 
{ 
     // 200x300 px window will apear in the center of the screen. 
     private Rect windowRect = new Rect ((Screen.width - 200)/2, (Screen.height - 300)/2, 200, 300); 
     // Only show it if needed. 
     private bool show = false; 

     public CamCaptureDialogBehavior() 
     { 
     } 
     // Use this for initialization 
     void Start() 
     { 

     } 

     void OnGUI() 
     { 
       if (show) 
         windowRect = GUI.Window (0, windowRect, DialogWindow, "Game Over"); 
     } 

     void DialogWindow (int windowID) 
     { 
       float y = 20; 
       GUI.Label (new Rect (5, y, windowRect.width, 20), "Title goes here"); 

       if (GUI.Button (new Rect (5, y, windowRect.width - 10, 20), "Ok")) { 
         Application.LoadLevel (0); 
         show = false; 
       } 
     } 

     // To open the dialogue from outside of the script. 
     public void Open() 
     { 
       show = true; 
     } 
     // Update is called once per frame 
     void Update() 
     { 

     } 
} 

동작 : PictureButtonBehavior :

using UnityEngine; 
using UnityEditor; 
using System.Collections; 

public class PictureButtonBehavior : MonoBehaviour 
{ 
     private bool displayedGUI = false; 
     private bool ShowThisGUI = false; 

     void Start() 
     { 
     } 

     void Update() 
     { 
       if (displayedGUI == true) { 
         Debug.Log (string.Format ("displayedGUI = {0}\r\n", displayedGUI)); 
         displayedGUI = false; 
         ShowThisGUI = false; 
       } 
     } 

     void OnGUI() 
     { 
       if (ShowThisGUI) { 
         Debug.Log (string.Format ("ShowThisGUI = {0}\r\n", ShowThisGUI)); 
         displayedGUI = true; 
         ShowThisGUI = false; 
         CamCaptureDialogBehavior ccdb = new CamCaptureDialogBehavior(); 
         if (ccdb != null) { 
           ccdb.enabled = true; 
           ccdb.Open(); 
         } 
       } 
     } 

     public void OnClick() 
     { 
       ShowThisGUI = true; 
     } 
} 

CamCaptureDialogBehavior ccdb = new CamCaptureDialogBehavior();에서 ccdb 항상 null입니다 아래의 두 가지 동작을 고려하십시오.

Unity/Mono에서 클래스를 인스턴스화하는 데 필요한 확실한 방법이 있습니까?

또는 CamCaptureDialogBehaviorPictureButtonBehavior에 어떻게 인스턴스화하고 CamCaptureDialogBehavior으로 표시되는 대화 상자를 표시 할 수 있습니까?

+1

Monobehaviour에서 파생되는 경우 new를 사용하지 마십시오. intiantiate 사용 -> http://docs.unity3d.com/ScriptReference/Object.Instantiate.html – OMGtechy

+0

나는 initiate를 사용하려했지만 initiate를 사용하기 위해 처음부터 초기화 된 값이 필요합니다. 그것은 여전히 ​​나 null을 반환합니다. – Abhinav

+1

복제 할 인스턴스가 없다면 Resources.Load를 사용할 수 있습니다. – OMGtechy

답변

1

MonoBehavioursnew을 호출 할 수 없습니다.

스크립트가 첨부 된 프리 팹을 인스턴스화 할 수 있습니다.

GameObject g = Instantiate(prefab) as GameObject; 

또는 기존 게임 객체에 추가 할 수 있습니다.

gameObject.AddComponent<ScriptName>(); 

다음 당신은 조립식 무엇 을 요청합니다. 이것은 매우 단순하지만 Unity에서 매우 강력한 것입니다. 하나 만드는 방법에 대해서는 Short tutorial을 참조하십시오.

당신은 자산을 선택하여 조립식를 만들 수는> 조립식을 만든 다음 은 "빈"조립식 자산 나타납니다에 현장에서 개체를 드래그. 프리뷰 애셋을 프로젝트 뷰에서 씬 뷰로 드래그하기 만하면 프리 팹 인스턴스가 생성됩니다.

+0

이 작업을 수행 할 수 없습니다 (너무 새로 작성 : P). 올바른 리소스를 가리켜 주셔서 감사합니다. 문제를 해결했지만 미망인을 동적으로 생성했습니다. – Abhinav

관련 문제