简体   繁体   English

无法从具有脚本的空游戏对象获取变量

[英]can't get variable from an empty game object with script

I'm trying to get variable from an empty gameObject's script, but I can't assign that gameObject on the inspector. 我试图从一个空的gameObject的脚本中获取变量,但无法在检查器上分配该gameObject。 These are the screen shots and codes from my game. 这些是我的游戏的屏幕截图和代码。

Well, I have this code to load when the game is starting. 好吧,我有这个代码在游戏开始时加载。 Land and Prince are objects that made from this code. Land和Prince是根据此代码制成的对象。

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


public class loadGame : MonoBehaviour 
{
    public static loadGame loadSave;

    public GameObject objPrince;
    public Pangeran charPrince;
    public Transform prefPrince;

    public Sprite[] spriteTanah;
    public Dictionary<string, Tanah> myTanah = new Dictionary<string, Tanah>();
    public Dictionary<string, GameObject>objTanah = new Dictionary<string, GameObject>();
    public Tanah tempTanah;
    public GameObject tempObjTanah;
    public Transform prefTanah;
    public float mapX;
    public float mapY;
    public int i = 0;
    public int j = 0;
    public int rows = 9;
    public int column = 9;

    void Awake(){
        if(loadSave == null)
        {
            DontDestroyOnLoad(gameObject);
            loadSave = this;
        }
        else if(loadSave != this)
            Destroy(gameObject);

    }

    // Use this for initialization
    void Start () 
    {
        Load ();   
    }

    // Update is called once per frame
    void Update () 
    {       
    }

    public void Load()
    {
        if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {

        }
        else
        {
            charPrince = new Pangeran ("Prince", "04Okt1993", 0, 0, 0, 0, 0, false, false);
            objPrince = GameObject.Instantiate (prefPrince, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
            //objPrince.name = "Prince";
            charPrince.locationY = 0f;
            charPrince.locationX = 0f;
            charPrince.hadapAtas = false;
            charPrince.hadapKanan = true;
            charPrince.stamina = 100f;
            charPrince.exp = 0f;
            charPrince.speed = 0f;

            for(i = 0 ; i < rows ; i ++)
            {
                for(j = 0; j<column ; j++)
                {
                    mapX = (i-j) * 0.8f;
                    mapY = (i+j) * 0.4f;

                    if(i>=1 && j>=1 && i<=5 && j<=5)
                    {
                        prefTanah.name = "land-"+j.ToString("0#")+"-"+i.ToString("0#");
                        tempTanah = new Tanah("land-"+j.ToString("0#")+"-"+i.ToString("0#"),mapX,mapY,"land",spriteTanah[0],spriteTanah[1],spriteTanah[2]);
                        myTanah.Add("land-"+j.ToString("0#")+"-"+i.ToString("0#"),tempTanah);
                        tempObjTanah = GameObject.Instantiate(prefTanah, new Vector3(mapX,mapY,0),Quaternion.identity)as GameObject;
                        objTanah.Add("land-"+j.ToString("0#")+"-"+i.ToString("0#"),tempObjTanah);
                    }
                    else
                    {
                        prefTanah.name = "snow-"+j.ToString("0#")+"-"+i.ToString("0#");
                        tempTanah = new Tanah("snow-"+j.ToString("0#")+"-"+i.ToString("0#"),mapX,mapY,"snow");
                        myTanah.Add("snow-"+j.ToString("0#")+"-"+i.ToString("0#"),tempTanah);
                        tempObjTanah = GameObject.Instantiate(prefTanah, new Vector3(mapX,mapY,0),Quaternion.identity)as GameObject;
                        objTanah.Add("snow-"+j.ToString("0#")+"-"+i.ToString("0#"),tempObjTanah);
                    }
                }
            }

        }
    }
}

I'm trying to access one of some variables from code above, but I can't assign it in the inspector. 我正在尝试从上面的代码访问某些变量之一,但无法在检查器中分配它。

我想在上面的代码中分配该代码

but I can't do it. 但是我做不到 而这发生了

please help me. 请帮我。 Thank you. 谢谢。

The problem is that the loadLand variable is of type LoadGame which is a script. 问题在于loadLand变量的类型为LoadGame,这是一个脚本。 What you are trying to do is to add a GameObject to this variable. 您要尝试的是将GameObject添加到此变量。 So change the public variable type to 因此,将公共变量类型更改为

public GameObject LoadLandObject;
private LoadGame loadLand;

and create a private LoadGame variable which is the reference to your script. 并创建一个私有LoadGame变量,该变量是对脚本的引用。 Add in the Start() method 添加Start()方法

loadLand = (LoadGame)LoadLandObject.GetComponent<LoadGame>();

With this you load the script LoadGame of the GameObject into the variable. 这样,您将GameObject的脚本LoadGame加载到变量中。

Do you ever set your plantingScript.loadLand to the instance of loadGame.loadSave ? 您是否将plantingScript.loadLand设置为loadGame.loadSave的实例? This must be done after Awake in your case to be sure the instance has been set. 在您的情况下,必须在“ Awake之后执行此操作,以确保已设置实例。

Can I ask, what are you trying to do? 我可以问一下,你想做什么?

You should simply just assign you loadGame script in the inspector of plantingScript and not use statics at all. 您只需要在plantingScript的检查器中分配loadGame脚本,而根本不使用静态变量。 They will bite your ass sooner or later (and I'm guessing it already is). 他们迟早会咬你的屁股(我猜是已经咬了)。

Have a similar problem. 有类似的问题。 Did n't find simple and clear solution. 找不到简单明了的解决方案。 May be what i did help to you. 可能是我为您提供的帮助。

  1. Create empty game object. 创建空的游戏对象。
  2. Attach loadGame.cs to it (if you whant to control when script starts - uncheck it,in this case don't foget to set loadLand.enabled to true in plantingScript when needed) 将loadGame.cs附加到它(如果您想控制脚本何时启动-取消选中它,在这种情况下,请不要在需要时在plantingScript中将loadLand.enabled设置为true)
  3. Drag this object to you loadLand field 将此对象拖动到您的loadLand字段

Sample project 样例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM