Unity3d 實例化對象


unity3D新手上路 實例化對象出現的bug

當我年輕的在unity3DC#文件中使用new來創建一個對象的時候。


		public class Single : MonoBehaviour {
		
		   private Single m_single;

	       m_single = new Single();

		}

然后它警告了我


    ...   You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.MonoBehaviours can only be added using AddComponent() ...

而且你會發現m_single一直為空。它告訴我們當你繼承了MonoBehaviour你就不能使用關鍵字new來實例化一個對象。

具體原來我也母雞。


		public class Single : MonoBehaviour {
		
		 private Single m_single;
		
		
		  void Awake(){
		       m_single = this;
		   }
		
		
		  public static Single M_single(){
		       return m_single;
		   }
		}

好了現在這個m_single就是一個Single的實例化對象。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM