我正在制作一个RTS风格的游戏,我有一个错误。 我正在尝试将当前所选单元(脚本所在的对象)发送到光线投射所对象的Playmaker FSM。我意识到你无法访问游戏对象并在静态函数内部进行转换,因此我尝试调用另一个函数来使用命中并填充gameobject变量。
这是错误:
Assets/Scripts/Unit.cs(57,41): error CS0120: An object reference is required to access non-static member `Unit.SetOurObject(UnityEngine.RaycastHit)'
我认为的主要问题是:
public static Vector3 GetDestination()
{
if (moveToDestination == Vector3.zero)
{
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(r, out hit))
{
while (!passables.Contains(hit.transform.gameObject.name))
{
if (!Physics.Raycast(hit.transform.position, r.direction, out hit)) //point + r.direction * 0.1f
break;
}
//gameObject.GetComponent<NavMeshAgent>().SetDestination(hit.point);
//if (hit.transform != null){
//print (hit);
if (resources.Contains(hit.transform.gameObject.name)){
SetOurObject(hit);
//SelectedUnit.Value = GameObject.name;
//ResourceHit.Fsm.Event("startHit");
} else {
moveToDestination = hit.point;
}
//}
}
}
return moveToDestination;
}
public void SetOurObject(RaycastHit hitRay)
{
ourObject = hitRay.transform.gameObject;
PlayMakerFSM ourFSM = ourObject.GetComponent<PlayMakerFSM>();
FsmGameObject SelectedUnit = ourFSM.FsmVariables.GetFsmGameObject("SelectedUnit");
SelectedUnit.Value = new GameObject();
ourFSM.Fsm.Event("ResourceHit");
}
以下是整个剧本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using HutongGames.PlayMaker;
public class Unit : MonoBehaviour {
public PlayMakerFSM ResourceHit;
public GameObject ourObject;
public bool selected = false;
private Color SelectedCol = new Color(0.114f, 0.22f, 0.039f, 1.0f);
private Color UnselectedCol = new Color(0.357f, 0.604f, 0.184f, 1.0f);
private bool selectedByClick = false;
private Vector3 moveToDest = Vector3.zero;
private static Vector3 moveToDestination = Vector3.zero;
private static List<string> passables = new List<string>() { "Floor" };
private static List<string> resources = new List<string>() { "Res_Wood" };
// Update is called once per frame
private void CleanUp()
{
if (!Input.GetMouseButtonUp(1))
moveToDestination = Vector3.zero;
}
private NavMeshAgent agent;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
public static Vector3 GetDestination()
{
if (moveToDestination == Vector3.zero)
{
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(r, out hit))
{
while (!passables.Contains(hit.transform.gameObject.name))
{
if (!Physics.Raycast(hit.transform.position, r.direction, out hit))
break;
}
if (resources.Contains(hit.transform.gameObject.name)){
SetOurObject(hit);
} else {
moveToDestination = hit.point;
}
}
}
return moveToDestination;
}
public void SetOurObject(RaycastHit hitRay)
{
ourObject = hitRay.transform.gameObject;
PlayMakerFSM ourFSM = ourObject.GetComponent<PlayMakerFSM>();
FsmGameObject SelectedUnit = ourFSM.FsmVariables.GetFsmGameObject("SelectedUnit");
SelectedUnit.Value = new GameObject();
ourFSM.Fsm.Event("ResourceHit");
}
void Update () {
CleanUp();
if (this.GetComponent<Renderer>().isVisible && Input.GetMouseButton(0))
{
if (!selectedByClick){
Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
camPos.y = CameraOperator.InvertMouseY (camPos.y);
selected = CameraOperator.selection.Contains(camPos);
}
if (selected)
this.GetComponent<Renderer> ().material.color = UnselectedCol;
else
this.GetComponent<Renderer> ().material.color = SelectedCol;
}
if (selected && Input.GetMouseButtonUp(1))
{
Vector3 destination = GetDestination();
if (destination != Vector3.zero)
{
gameObject.GetComponent<NavMeshAgent>().SetDestination(destination); //all you need if you have unity pro
//moveToDest = destination;
//moveToDest.y += floorOffset;
}
}
}
private void OnMouseDown()
{
selectedByClick = true;
selected = true;
}
private void OnMouseUp()
{
if (selectedByClick)
selected = true;
selectedByClick = false;
}
}
}
提前致谢! =)
答案 0 :(得分:3)
尽管Christos对why
是正确的,但它会抛出一个异常(你试图访问一个实例方法就好像它是一个静态方法),但是他缺少一个细节。
在Unity3D中,您无法实例化(轻松)实现MonoBehaviour的类。您可以通过将脚本组件附加到现有游戏对象来创建它们,然后您可以在代码中引用它们。
因此,要解决此问题,如果要调用该方法,则必须先获取对场景中附加的脚本组件的引用,然后才能执行此操作。
简单的例子,让我们说脚本组件Unit连接到同一个GameObject,你可以像这样引用它:
Unit unit = GetComponent<Unit>();
// now we can call instance fields/properties/methods on this specific instance!
答案 1 :(得分:0)
从您收到的错误消息中可以看出方法
SetOurObject(UnityEngine.RaycastHit)
不是类static
的{{1}}方法。
因此,您首先要创建此类的实例,然后调用此方法。
Unit
然后你可以使用这个对象调用方法。
// I don't know exactly the signature of the constructor class
// If it is parameterless or not etc.
// So you have to correct it correspondingly, If I am wrong.
var unit = new Unit();