Access to a variable inside a C# class

Hello everybody,

I’m having trouble accesing a variable inherited from a class.

This is my class:

using UnityEngine;
    using System.Collections;
    
    public class RecetaInitializer : MonoBehaviour {
    
    	public RecetaConstructor[] Receta;
    
    	void Start () {
    		Receta = new RecetaConstructor[9];
    
    		for (int i = 0; i < Receta.Length; i++)
    		{
    			Receta *= new RecetaConstructor();*
  •  }*
    
  •  Receta[0].AddReceta(1, "Huevo Duro", "agua,huevo");*
    
  •  Receta[1].AddReceta(2, "Papas Fritas", "papa,aceite");*
    
  •  Receta[2].AddReceta(3, "Omelette", "huevo,queso,jamon");*
    
  •  Receta[3].AddReceta(4, "Licuado De Banana", "leche,banana");*
    
  •  Receta[4].AddReceta(5, "Chocolatada", "leche,chocolate");*
    
  •  Receta[5].AddReceta(6, "Fideos Con Queso", "fideos,queso");*
    
  •  Receta[6].AddReceta(7, "Pancho", "pan,salchicha,queso");*
    
  •  Receta[7].AddReceta(8, "Pizza", "pan,queso,jamon,tomate");*
    
  •  Receta[8].AddReceta(9, "Hamburguesa", "pan,paty,queso,lechuga,tomate");* 
    
  •  // Debug.Log(Receta[7].GetRecetaName());*
    
  • }*

}
Now, I want to access from another script the variable Receta. To achieve this I created another class that I use to manage my game:
using UnityEngine;
using System.Collections;

public class MenuGUI : RecetaInitializer{

  • public RecetaConstructor Receta;*

  • // RecetaConstructor is a class I have which works fine, it lets me create different recipes for my game.*

  • void Start()*

  • {*

  •  Receta = gameObject.GetComponent<RecetaInitializer>().Receta;*
    
  •  PrepareGame();*
    
  • }*

  • private void PrepareGame()*

  • {*

  •  Debug.Log(Receta[0].GetRecetaName());*
    
  •  // GetRecetaName() is self-explanatory, given the index it returns the name of the Recipe/Receta*
    
  • }*

}
I’m clearly missing something here, ad Unity gives me a nice “Object reference not set to an instance of an object”.
I also tried by not inheriting the class and add it with AddComponent and then RecetaConstructor[] Receta = gameObject.GetComponent().Receta; But the result is the same.
Any hint?
Thanks very much!

Ok, just remove the line

Receta = new RecetaConstructor[9]; 

from the start method in RecetaInitializer.

and replace

public RecetaConstructor[] Receta;

with

public RecetaConstructor[] Receta = new RecetaConstructor[9];

That line has been blanking your preset data each time.

And make sure to rename your Start() in RecetaInitializer to Awake().

why do you need to initialize data in Start() looks like you data is hardcoded and static, so just use static initialization

using UnityEngine;
using System.Collections;

public class RecetaInitializer : MonoBehaviour {

public static RecetaConstructor[] Receta = new RecetaConstructor[9]{
    new Receta(1, "Huevo Duro", "agua,huevo"),
new Receta(2, "Papas Fritas", "papa,aceite"),
new Receta(3, "Omelette", "huevo,queso,jamon"),
new Receta(4, "Licuado De Banana", "leche,banana"),
new Receta(5, "Chocolatada", "leche,chocolate"),
new Receta(6, "Fideos Con Queso", "fideos,queso"),
new Receta(7, "Pancho", "pan,salchicha,queso"),
new Receta(8, "Pizza", "pan,queso,jamon,tomate"),
new Receta(9, "Hamburguesa", "pan,paty,queso,lechuga,tomate")
};
        
}

or, if you don’t like it, make use getter and setter and in getter just check if you private array=null initialize them.