Search code examples
c#unity-game-enginegameobject

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. 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.

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.

I want to assign that code above in this code

but I can't do it. and this is happened

please help me. Thank you.


Solution

  • The problem is that the loadLand variable is of type LoadGame which is a script. What you are trying to do is to add a GameObject to this variable. 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. Add in the Start() method

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

    With this you load the script LoadGame of the GameObject into the variable.