【问题标题】:error CS0176: Member 'Camera.main.current' cannot be accessed with an instance reference; qualify it with a type name instead in unity c# [duplicate]错误 CS0176:无法使用实例引用访问成员“Camera.main.current”;用类型名称而不是统一c#来限定它[重复]
【发布时间】:2021-08-25 19:16:16
【问题描述】:

我试图在按下 LeftMouseButton 时在鼠标位置创建一个 GameObject 实例,我试图将其转换为世界位置,但出现错误:无法访问成员“Camera.main.current”带有实例引用;改为使用类型名称来限定它

public GameObject tower;  
   public Camera sceneCamera;
   Vector3 objectP;   
    void Update () 
     {
        sceneCamera = Camera.main;
        if (sceneCamera!= null) 
        {
            Vector3 mousePos = Input.mousePosition;
         mousePos.z = 2.0f;
        Vector3 objectP = Camera.main.current.ScreenToWorldPoint(mousePos);
        }
         
     if (Input.GetButtonDown("Fire1")) 
         {
         Instantiate(tower,objectP, Quaternion.identity);
         }
     }
    
   
}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

Camera.main.current 替换为Camera.main

maincurrent 都是 Camera 类的静态属性,因此像您正在做的那样链接它们是没有意义的(实际上是不允许的)。

【讨论】:

  • Camera.current 不应在这种情况下使用。来自文档for low-level render control only
  • 你说得对,我编辑了答案。
猜你喜欢
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多