非静态字段需要对象引用 [英] An object reference is required for the non-static field

查看:81
本文介绍了非静态字段需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用UnityEngine

; 

公共类ModMenuScript:MonoBehaviour {

public bool hack1;
public bool hack2;
public bool hack3;
public string string1;
public string string2;
public string string3;
public bool ShowHide = false;

void OnGUI(){

if(GUI.Button(new Rect(20,20,130f,30f),SHOW / HIDE))
{
ShowHide =!ShowHide;
}

if(ShowHide)
{
GUI.Box(new Rect(20,50,180,150),iAndroHacker的mod菜单);
if(GUI.Button(new Rect(25,80,170f,30f),string1))
{
hack1 =!hack1;
}
if(GUI.Button(new Rect(25,115,170f,30f),string2))
{
hack2 =!hack2;
}
if(GUI.Button(new Rect(25,150,170f,30f),string3))
{
hack3 =!hack3;
}
}

if(hack1)
{
string1 =Unlimited armor< color = green> ON< / color>;
hack1 = false;
}
其他
{
string1 =无限护甲< color = red> OFF< / color>;
hack1 = true;
}

if(hack2)
{
string2 =Unlimited team health< color = green> ON< / color>;
hack2 = false;
}
其他
{
string2 =无限团队健康状况< color = red> OFF< / color>;
hack2 = true;
}

if(hack3)
{
string3 =Unlimited coins< color = green> ON< / color>;
hack3 = false;
}
其他
{
string3 =无限硬币< color = red> OFF< / color>;
hack3 = true;
}
}
}









我收到错误
非静态字段,方法或属性'ModMenuScript.ShowHide'main.cs

需要对象引用仍然它显示一切





我尝试过:



我试图编译



它显示错误

解决方案

它看起来你在你的程序 main 函数中引用了 ModMenuScript.ShowHide 。由于 main 静态,因此无法访问非静态 ShowHide property:您需要 ModMenuScrip 类的实例(对象)才能访问它。类似

 静态  void  main( )
{
ModMenuScript mms = new ModMenuScript();
// ..
mms.ShowHide = ;
// ..
}


using UnityEngine;

public class ModMenuScript : MonoBehaviour {

    public bool hack1;
    public bool hack2;
    public bool hack3;
    public string string1;
    public string string2;
    public string string3;
    public bool ShowHide = false;

    void OnGUI() {

        if (GUI.Button(new Rect(20, 20, 130f, 30f), "SHOW/HIDE"))
        {
            ShowHide = !ShowHide;
        }

        if (ShowHide)
        {
            GUI.Box(new Rect(20, 50, 180, 150), "iAndroHacker's mod menu");
            if (GUI.Button(new Rect(25, 80, 170f, 30f), string1))
            {
                hack1 = !hack1;
            }
            if (GUI.Button(new Rect(25, 115, 170f, 30f), string2))
            {
                hack2 = !hack2;
            }
            if (GUI.Button(new Rect(25, 150, 170f, 30f), string3))
            {
                hack3 = !hack3;
            }
        }

        if (hack1)
        {
            string1 = "Unlimited armor <color=green>ON</color>";
            hack1 = false;
        }
        else
        {
            string1 = "Unlimited armor <color=red>OFF</color>";
            hack1 = true;
        }

        if (hack2)
        {
            string2 = "Unlimited team health <color=green>ON</color>";
            hack2 = false;
        }
        else
        {
            string2 = "Unlimited team health <color=red>OFF</color>";
            hack2 = true;
        }

        if (hack3)
        {
            string3 = "Unlimited coins <color=green>ON</color>";
            hack3 = false;
        }
        else
        {
            string3 = "Unlimited coins <color=red>OFF</color>";
            hack3 = true;
        }
    }
}





I got An error 
	An object reference is required for the non-static field, method, or property 'ModMenuScript.ShowHide' main.cs

still it is showing for everything 



What I have tried:

I have tried to compile

it shows that error

解决方案

It looks you are referencing ModMenuScript.ShowHide in your program main function. Since main is static it cannot access the non-static ShowHide property: you need an instance (an object) of the ModMenuScrip class in order to access it. Something like

static void main()
{
  ModMenuScript mms = new ModMenuScript(); 
  //..
  mms.ShowHide = true;  
  //..
}


这篇关于非静态字段需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆