I'm really not sure whats wrong...
我真的不确定什么是错的......
im following along to a YouTube Video because this code is much too advanced for me. I dont know if its a problem with unity 5 or whatever but it just keeps giving me this error: error CS0120: An object reference is required to access non-static member `UnityEngine.Camera.ScreenPointToRay(UnityEngine.Vector3)'
我正在关注YouTube视频,因为这段代码对我来说太先进了。我不知道它与统一5或任何一个问题,但它只是不断给我这个错误:错误CS0120:一个对象引用才能访问非静态成员`UnityEngine.Camera.ScreenPointToRay(UnityEngine.Vector3)”
I know its probably a bad idea to copy from YouTube without knowing what it means but im just trying to get a script for touchscreen stuff.
我知道从YouTube上复制而不知道它意味着什么可能是一个坏主意,但我只想尝试获取触摸屏内容的脚本。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
void Update () {
#if UNITY_EDITOR
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = Camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,touchInputMask)){
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if(Input.GetMouseButtonDown(0)){
recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if(Input.GetMouseButtonUp(0)){
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if(Input.GetMouseButton(0)){
recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
foreach (GameObject g in touchesOld){
if(!touchList.Contains(g)){
g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
#endif
if (Input.touchCount > 0) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches) {
Ray ray = Camera.ScreenPointToRay(touch.position);
if(Physics.Raycast(ray,out hit,touchInputMask)){
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if(touch.phase == TouchPhase.Began){
recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if(touch.phase == TouchPhase.Ended){
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if(touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){
recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
if(touch.phase == TouchPhase.Canceled){
recipient.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld){
if(!touchList.Contains(g)){
g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
1 个解决方案
#1
I'm not a Unity expert, but the documentation says that ScreenPointToRay is an instance method, not a static method. This means that you would have to create an instance of Camera before using it. So, perhaps:
我不是Unity专家,但文档说ScreenPointToRay是一个实例方法,而不是静态方法。这意味着您必须在使用之前创建Camera实例。所以,也许:
camera = GetComponent<Camera>();
Ray ray = camera.ScreenPointToRay(touch.position);
Note that there are two places in your code snippet where you'd need to make this change.
请注意,您的代码段中有两个位置需要进行此更改。
#1
I'm not a Unity expert, but the documentation says that ScreenPointToRay is an instance method, not a static method. This means that you would have to create an instance of Camera before using it. So, perhaps:
我不是Unity专家,但文档说ScreenPointToRay是一个实例方法,而不是静态方法。这意味着您必须在使用之前创建Camera实例。所以,也许:
camera = GetComponent<Camera>();
Ray ray = camera.ScreenPointToRay(touch.position);
Note that there are two places in your code snippet where you'd need to make this change.
请注意,您的代码段中有两个位置需要进行此更改。