0
\$\begingroup\$

I am instantiating and parenting two objects - the first is a Unity primitive and the second is a prefab cube (literally nothing but a cube made into a prefab). The direct instantiation of the primitive works great, but when I try to instantiate the prefab I get NullReferenceException:Object reference not set to an instance of an object

Below is my script:

    Vector3 myPosition = new Vector3 (x, Rotator_Prefab.transform.position.y , z);
    GameObject ball = GameObject.CreatePrimitive (PrimitiveType.Sphere);
    ball.transform.parent = gameObject.transform; // no problems and gameObject.name gives me the right parent


    GameObject prefab = Instantiate (Rotator_Prefab, myPosition, Quaternion.identity) as GameObject;
    prefab.transform.parent = gameObject.transform; // this gives me an error

The error is coming from the prefab still being null after being instantiated. Still it shows up in the scene. Would anyone have any ideas ? - Thank you!

Update: Where the prefab is coming from

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I am positive the parent isn't null, because the primitive object finds it and also when I print(gameObject.name) it gives me the right parent... The parent is there, but the prefab doesn't acknowledge it \$\endgroup\$ – BonnieB Jul 21 '16 at 10:07
  • \$\begingroup\$ You are instantiating the root node here. There is no parent, it would be impossible for the "prefab" object to have a parent here. Not sure why you would assume otherwise. \$\endgroup\$ – jgallant Jul 21 '16 at 10:24
  • \$\begingroup\$ Hi Jon, I don't think I understand what you mean. My script has a public Gameobject variable where I drag&drop the prefab. The script itself is attached to a Gameobject (i.e the intended parent) - I instantiate the prefab and the primitive from the same place in the script .. \$\endgroup\$ – BonnieB Jul 21 '16 at 10:41
  • \$\begingroup\$ That instantiated "prefab" object in your code above, would not have a parent, as you just instantiated it and it isn't associated with another gameObject at that point. It doesn't matter where you instantiate it from. \$\endgroup\$ – jgallant Jul 21 '16 at 10:43
  • 1
    \$\begingroup\$ Put a breakpoint, run your code in a debugger, and check what object is null! \$\endgroup\$ – Alexandre Vaillancourt Jul 21 '16 at 11:14
1
\$\begingroup\$

For what I've seen in the comments your prefab is null because there is nothing assigned at runtime.

Drag n dropping stuff in the inspector of your script is ignored by the engine at scene load. Once your script is assigned as component to a gameObject in your hiearchy then public slots will appear and you should do there your drag n drop.

Otherwise you can dynamically load via script doing

Rotator_Prefab = Resources.Load<Gameobject>(*your prefab path*);

After you edit I should specify that your variable must be of type GameObject to instantiate it correctly.

\$\endgroup\$
  • \$\begingroup\$ I think we should clarify that it only needs to be a GameObject because the code around the Instantiate() is expecting a GameObject out in this particular example. Instantiate() works with any type derived from UnityEngine.Object (including Components & Monobehaviours, Materials, etc), so it's not only GameObjects that can be correctly instantiated as a general rule. \$\endgroup\$ – DMGregory Jul 21 '16 at 14:23
0
\$\begingroup\$

In the end, keeping the code from the question and changing the public field of the Prefab to type GameObject (not Transform) did the trick.

(I did not need to use Resources.Load("PATH");)

I hope this saves someone some time

\$\endgroup\$
  • \$\begingroup\$ If you want more context for why this happened, it's the use of as GameObject after the Instantiate - the type of the object you pass to Instantiate is the type you'll get back (useful if you want to use a specific type for your prefab field, like Rigidbody if it needs physics, and get back a reference to the Rigidbody on the spawned instance without a separate GetComponent step). Since your prefab was a Transform, the as GameObject replaced the return value with null. This would also work: Transform instance = (Transform)Instantiate(prefabThatIsATransform); instance.parent = ... \$\endgroup\$ – DMGregory Jul 21 '16 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.