- Home /
Getting a component of an instantiated object returns an error
Hi, I'm trying to get a component of an instantiated object in C# but it keeps returning errors whenever I try it. I have a prefab with a C# script component called MenuButton and I'm using this code to instantiate it and get the menu button component:
GameObject b = Instantiate(optionPrefab, transform.position, Quaternion.identity) as GameObject;
MenuButton b_btn = b.GetComponent<MenuButton>();
but I keep getting the same error:
NullReferenceException: Object reference not set to an instance of an object
CreateStartOptions.Start () (at Assets/CreateStartOptions.cs:16)
So to me it looks like the C# script or gameobject itself hasn't yet been registered during the instantiate call and it doesn't find the components of the object until the next frame. This same code always worked for me in UnityScript but since I've moved over to C# it's given me the same error consistently.
How can I insure that I can get the component upon instantiation?
try an as $$anonymous$$enuButton after the method invocation?
$$anonymous$$enuButton b_btn = b.GetComponent<$$anonymous$$enuButton>() as $$anonymous$$enuButton;
@beeboks Which line is line 16?
The code you've shown us wouldn't throw a NRE just because it can't find the $$anonymous$$enuButton component (it would just set b_btn to null in that case). It looks to me more likely that b itself is null.
Answer by POiD · Mar 02, 2015 at 02:12 PM
I do this quite often in my scripts using similar logic as you do, I just cast to a gameobject [(GameObject)] instead of using "as", but I doubt that's the issue.
Are you sure that optionPrefab is assigned to a GameObject variable in your script where you are trying to instantiate it? As in did you make the variable public and/or use [SerializeField] and then drag a copy of the Prefab into the variable spot in Unity Inspector?
[SerializeField] GameObject optionPrefab;
The other option is are you sure of the spelling of your Script attached to your prefab? As in you are accessing a script "MenuButton"?
Have you tried with a standard component added to the prefab, such as a BoxCollider for example?
GameObject newObject = (GameObject)Instantiate(optionPrefab);
BoxCollider ourBox = newObject.GetComponent<BoxCollider>();
Yeah, I had all of the hookups in. I didn't use [SerializeField]. What does that do?
I got it working now by casting the Instantiated object as a Transform ins$$anonymous$$d of a GameObject.
Transform newObject = (Transform)Instantiate(optionPrefab);
$$anonymous$$enuBtn m_btn = newObject.gameObject.GetComponent<$$anonymous$$enuBtn>();
Do you know why that would work but GameObject wouldn't?
Hi Beeboks, I have no idea. In my code I'm always casting to a GameObject and it works. Did you try casting to a GameObject rather than use "as" ?
SerializeField: http://docs.unity3d.com/ScriptReference/SerializeField.html
From my understanding this allows you to set the field via the Editor, however doesn't require it to be Public, so that other scripts couldn't just access the variable. Came across it in a tutorial somewhere.