无法在静态上下文中访问非静态方法 "GetComponent",尽管我没有将 class 声明为静态
cannot access non static method "GetComponent" in static context, despite me not declaring the class as static
我目前正在努力使 class 可以从其他脚本访问。以下是我编写的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
// Start is called before the first frame update
public class _move
{
private float _sizex;
private float _sizey;
public _move(float sizx, float sizy....etc)
{
_sizex = sizx;
_sizey = sizy;
}
public void regularmove(float sizex, float sizey...etc)
{
GameObject _player;
_player = GameObject.GetComponent<GameObject>();
BoxCollider2D bc2d;
bc2d = _player.GetComponent <BoxCollider2D>();
bc2d.offset = new Vector2(locationx + 1, locationy);
}
}
然而,当我尝试编译它时,它给了我一个错误。
cannot access non-static method"GetComponent"
在这行代码上
_player = GameObject.GetComponent<GameObject>();
我不知道为什么会这样,因为我还没有将 class 声明为静态的,并且不太了解 GetComponent 的属性。有人能告诉我发生了什么以及我该如何解决这个问题吗?
另外,当我改变
GameObject _player;
to
public GameObject player;
下面的脚本突然停止工作,给我这样的错误
cannot resolve symbol _player
这里究竟发生了什么?
提前致谢!
创建 _move class 实例的任何脚本(顺便说一句,这是错误的命名,应该是 Move)也应该将其游戏对象传递给构造函数。
// attributes here are not valid if your class is not a MonoBehaviour
public class Move
{
GameObject m_object;
public Move(GameObject obj, ...)
{
m_object = obj;
}
}
如果您的 class 是一个 MonoBehaviour 组件,那么它已经有一个 GameObject 成员,您可以使用属性:
[RequireComponent(typeof(BoxCollider2D),typeof(Rigidbody2D))]
// Start is called before the first frame update
public class Move : MonoBehaviour
{
void Start()
{
Debug.Log(gameObject.name);
}
}
我目前正在努力使 class 可以从其他脚本访问。以下是我编写的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
// Start is called before the first frame update
public class _move
{
private float _sizex;
private float _sizey;
public _move(float sizx, float sizy....etc)
{
_sizex = sizx;
_sizey = sizy;
}
public void regularmove(float sizex, float sizey...etc)
{
GameObject _player;
_player = GameObject.GetComponent<GameObject>();
BoxCollider2D bc2d;
bc2d = _player.GetComponent <BoxCollider2D>();
bc2d.offset = new Vector2(locationx + 1, locationy);
}
}
然而,当我尝试编译它时,它给了我一个错误。
cannot access non-static method"GetComponent"
在这行代码上
_player = GameObject.GetComponent<GameObject>();
我不知道为什么会这样,因为我还没有将 class 声明为静态的,并且不太了解 GetComponent 的属性。有人能告诉我发生了什么以及我该如何解决这个问题吗? 另外,当我改变
GameObject _player;
to
public GameObject player;
下面的脚本突然停止工作,给我这样的错误
cannot resolve symbol _player
这里究竟发生了什么? 提前致谢!
创建 _move class 实例的任何脚本(顺便说一句,这是错误的命名,应该是 Move)也应该将其游戏对象传递给构造函数。
// attributes here are not valid if your class is not a MonoBehaviour
public class Move
{
GameObject m_object;
public Move(GameObject obj, ...)
{
m_object = obj;
}
}
如果您的 class 是一个 MonoBehaviour 组件,那么它已经有一个 GameObject 成员,您可以使用属性:
[RequireComponent(typeof(BoxCollider2D),typeof(Rigidbody2D))]
// Start is called before the first frame update
public class Move : MonoBehaviour
{
void Start()
{
Debug.Log(gameObject.name);
}
}