- Home /
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[i] = 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!
gameObject.GetComponent()
will be null unless there is a RecetaInitializer on the object. It will not pick up a child class of RecetaInitializer.
I'm not sure this is causing your problem. But as you didn't say what line the error was occurring on it's my best guess.
Sorry, the line was 'Debug.Log(Receta[0].GetRecetaName());'.
And I added the component through the inspector to the same GameObject that has this script, so it shouldn't be null.
Is the script attached to the gameobject RecetaInitializer or $$anonymous$$enuGUI ?
if it's $$anonymous$$enuGUI then the line
Receta = gameObject.GetComponent().Receta;won't find anything.
Both RecetaInitializer.cs and $$anonymous$$enuGUI.cs are attached to the same camera. That's pretty much the scene. And the class RecetaConstructor (which seems to work fine) is not attached to anything as I didn't inherited $$anonymous$$onoBehaviour on it, I just made it public.
Answer by MarkFinn · Nov 21, 2012 at 06:45 AM
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().
Just to add, unless there's more to the classes than you are showing us then the inheritance is really unnecessary and, frankly, only going to cause confusion.
It's working perfeclty now @markfinn , thank you very much. I still don't understand exactly what the problem was, but I'm looking forward to learn more of it as this was my first attemp at classes.
Also, there's not a lot more that this, I just thought that classes where the way to go on something like this, as I wanted to store data and using 3 different arrays seemed a little odd at the time.
in your first listing problem was that you overrided the method Start(), and didn't call this metod from your derrived class if you want to fix your previous code in $$anonymous$$enuGUI class in metod Start at first call base.Start(), so only after that it will cal your Start function of your base class.
One thing to note. The actual problem here was that when two classes on the same object depend on eachother's Start() functions the data will not be set. Start() is done before the first time Update() is run.
If one monobehaviour has data that another will use, make sure that data is done in Awake().
Answer by nicloay · Nov 21, 2012 at 06:34 AM
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.
Thanks Nikolay, that's definetely a better way of doing it and you're correct that my data is indeed static.
But the problem I have is still happening, whenever I try to access the Receta variable I get a null reference and I really don't know why.
sorry, mixed up a right part of archive initialization, already fixed in the code, must be public static RecetaConstructor[] Receta = new RecetaConstructor[9]{
Your answer
Follow this Question
Related Questions
Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer
C# Accessing Variables from Class Variable 1 Answer
Unity C# Start function without extending Mono 2 Answers
Accessing a variable inside a class inside other script... 2 Answers
object assigns but does not show when added to list, or any other var 0 Answers