0

I have two classes, genDicTable and StartGame. I want to reference a variable from genDicTable in StartGame, but it yields NULL.

genDicTable.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class genDicTable : MonoBehaviour
{
    public TextAsset file;
    public double masterCount;

    private void Start()
    {
        Load(file);

        masterCount = rowList.Count;
        Debug.Log(masterCount); // <-- This properly prints out the value of masterCount    
    }

    public class Row
    {
        public string id;
        public string word;
        public string length;
    }

    public List<Row> rowList = new List<Row>();

    public void Load(TextAsset csv) {
        // This function assigns a value into RowList
    }
}

StartGame.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartGame : MonoBehaviour {

    public genDicTable GEN;    

    private void Start()
    {
        Debug.Log(GEN.masterCount); // <-- This yields NULL.
    }
}

So, the problem is that when I access the variable masterCount in StartGame.cs, it yields an error "NullReferenceException: Object reference not set to an instance of an object."

What am I missing here?

Dongsan
  • 85
  • 1
  • 12
  • As it says the value is null. There is nothing wrong with this behavior. You have not created any instance for your variable GEN of type genDictTable in startgame.cs – Hasan Emrah Süngü Mar 18 '17 at 15:01
  • Please read carefully the pointed duplicate. If you don't know what is an instance and how to initialize it, then none of the answers given here could help you understand your problem and avoid future slips for the same reason – Steve Mar 18 '17 at 15:12

3 Answers3

3

You need the reference to that object.

If your class is attached to the same object that your StartGame, then you can do this: public genDicTable GEN = GetComponent<genDicTable>();

If your class is attached to a different object then public genDicTable GEN = GameObject.Find("YourOtherObjectName").GetComponent<genDicTable>();

EDIT:

If the genDicTable script exist and you just want to reference it:

public class StartGame : MonoBehaviour {

    public genDicTable GEN;    

    private void Start()
    {
        GEN = GameObject.Find("your object's name").GetComponent<genDicTable>();`
        Debug.Log(GEN.masterCount); // <-- This yields NULL.
    }
}

If the genDicTable script does not exist and you just want to create new instance of it then check this answer.

Community
  • 1
  • 1
FCin
  • 3,804
  • 4
  • 20
  • 49
  • Thanks, but your advice leads to "UnityException: Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead" error. I did call it in Start, too, and the value of my variable becomes 0, which is not the proper value. – Dongsan Mar 18 '17 at 15:14
  • @Dongsan modified his answer. Check that again. You have to do `GetComponent` inside a function. – Programmer Mar 18 '17 at 15:21
  • Thank you FCin and all other advisers. Finally I figured out what was wrong, The problem was the sequence that the two scripts are executed. It turned out that the referrer was executed before the referred. That's why I didn't get (or got error) the value I wanted. I changed Start() in genDicTable to Awake(), and the problem is solved. All, please forgive my dumbness. – Dongsan Mar 18 '17 at 16:05
-1
public static double masterCount;

Can't quite remember how the unity editor works but it looks as though you have not created an instance of genDicTable therefore the double needs to be static to be able to access.

-1

You never initialize the field. Change it to this

public genDicTable GEN = new genDicTable(); 

or do it in a constructor:

public StartGame()
{
    GEN = new genDicTable();
}
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • Thanks but new genDicTable() yields another warning saying "You are trying to create a MonoBehavior using the 'new' keyword. This is not allowed......" – Dongsan Mar 18 '17 at 15:12
  • Best would be to open another ticket for this problem. Unfortunately I never used `MonoBehaviour` and cannot help you with that. – Flat Eric Mar 18 '17 at 15:16
  • You [can't](http://stackoverflow.com/a/37399263/3785314) create new instance of script that inherits from `MonoBehaviour` with the `new` keyword. – Programmer Mar 18 '17 at 15:25