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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/atgczcl/article/details/52396805

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
     }
 }

代码必须要规范的写;

阅读更多
换一批

没有更多推荐了,返回首页