- Home /
Why does this Instantiate give a Null Reference Exception?
Hey Comm-unity, I am working on a project, and I can't seem to get it to stop returning a null reference exception. Here's the code:
Slide s = new Slide();
GameObject g = GameObject.Instantiate(ButtonPrefab, new Vector3(slides[slides.Count - 1].button.transform.position.x + xpos, slides[slides.Count - 1].button.transform.position.y + ypos, 0.0f), Quaternion.identity) as GameObject;
s.button = g.GetComponent<Button>();
s.button.transform.SetParent(ButtonParent.transform);
slides.Add (s);
And here's the error:
NullReferenceException: Object reference not set to an instance of an object
Manager.Add () (at Assets/Scripts/Manager.cs:110)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:137)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:601)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:743)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
And it claims that the error is on the line that says:
s.button = g.GetComponent<Button>();
Thank you in advance for your help! - Tiger27
Hello,
As always, if you don't give the error line. We can't really help you.
@$$anonymous$$ikilo $$anonymous$$y bad, let me fix it. Thanks!
I would recommend
Debug.Log(g);
Debug.Log(s.button);
s.button = g.GetComponent<Button>();
Debug.Log(s.button);
Debug.Log(s.button.transform);
s.button.transform.SetParent(ButtonParent.transform);
To see what is null but shouldn't. Can you check that?
Answer by Mikilo · Jan 14, 2017 at 11:34 PM
Hello!
Your case is explicit.
If the error is on the line:
s.button = g.GetComponent<Button>();
The null object is either s or g.
It would be surprising that s is null.
I would bet on g, if ButtonPrefab is wrong or null.
@$$anonymous$$ikilo Thanks for the answer. The problem was, I had made the ButtonPrefab a Button, and not a GameObject, meaning it was trying to access the Button component of a Button component, whcih obviously doesn't work, and therefore returned the error. The fix:
s.button = g;
Thanks, and have a nice day!
Your answer
Follow this Question
Related Questions
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
Instantiate as a child at position 2 Answers
Create a Transform without a GameObject 2 Answers
Controlling the update position of a cloned Gameobject C# 1 Answer
Instantiate Terrain Object as child of Empty Game Object 1 Answer