- Home /
Instatiate a Prefab, assign a public variable - get NullReferenceException
Hello Friends,
I have a prefab with an attached script to it. This script has a public variable of type "ConfigurationController".
I generate a load of these objects.
What I do is:
ConfigurationController confContr = gameObject.GetComponent<ConfigurationController>();
[...]
CustomScript script = go.GetComponent<CustomScript>();
script.confContr = confContr;
and then BAM: NullReferenceException: Object reference not set to an instance of an object loadOptions.Start () (at Assets/scripts/loadOptions.cs:26)
(line 26 is the script.confContr = confContr; of course :) ).
I really don't want each object to search for the component individually to save calculation.
[edit thanks @PAEvenson] Debug.Log(confContr) in the line directly before assigning shows: OptionsTable (ConfigurationController) UnityEngine.Debug:Log(Object) loadOptions:Start() (at Assets/scripts/Metagame/loadOptions.cs:26)
Thanks, Georg
Sadly not, sorry, I'll add the debug which proves that it's not null to the original post, give me a second
ahahaha can you please write that as an answer so I can mark yourself and mark myself stupid? No idea how I could've overlooked that - must've tunneled hard here.
Thanks!
Answer by PAEvenson · Jun 06, 2014 at 02:08 PM
if failed to find the CustomScript on the gameobject
CustomScript script = go.GetComponent<CustomScript>();
Look for the reasons why script is null. (Most likely it is not on the prefab :P)