Tag: unity3d 2dtools

Raycast导致NullReferenceException错误

产生错误的代码: void Update() { if (Input.touchCount > 0) { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero); if (hit && hit.collider != null && hit.collider.name == "leftTapArea") { hit.transform.name = "Hit"; } } } 它说这个string有什么问题: RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position),Vector2.zero); 错误: NullReferenceException:未将对象引用设置为对象的实例leftScript.Update()(位于Assets / leftScript.cs:16处)