get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initia

get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead.

这个问题的出现,千万不能出现,出现就说明你的代码质量不够高,stupidcode man!!! or girl…

第一种:

Error code:
private Transform mainCameraTr = Camera.main.transform;

Right Code:
private Transform mainCameraTr ;

void Start(){
    mainCameraTr = Camera.main.transform;
}

第二种:

public class RotateAroundSelf : MonoBehaviour {

     public RotateAroundSelf(float foo)  // Class class constructor
     {
         Vector3 speed = new Vector3(0,foo,0); //now it's evil
     }
 }

代码必须要规范的写;

你可能感兴趣的:(unity3d,unity3d开发,unity3d,c#)