I have a prefab of a button (btn) that I need to set parent (_canvos) for it after it spawned (online)...

I have try this code:

public Transform btn;
public Transform mkn;
public Image _canvos;

public void test ()
    Transform InTank = Network.Instantiate (btn, mkn.transform.position, Quaternion.identity, 0)as Transform;

But it doesn't work.

error: MissingReferenceException: The object of type 'Object' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. what's the problem??

  • 1
    \$\begingroup\$ you are trying to instantiate a gameobject over the network. I think you should provide where this code is running and which type of network architecture you are using. Basically this call would return the instance of the object created only on the client/server who is executing the code. By the way I would suggest you anyway to use the line btn.parent = _canvos.transform; instead of SetParent(). \$\endgroup\$
    – Leggy7
    Commented Aug 11, 2016 at 9:50
  • \$\begingroup\$ @Leggy7 Hi. now another error :"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption" and I don't have any problem with network. thanks \$\endgroup\$
    – mmoj
    Commented Aug 11, 2016 at 9:59
  • \$\begingroup\$ From what I see those public variable has prefabs dragged into. You can set the parent of instantiated objects only to other objects in the hirearchy. So be sure to have _canvos instantiated before setting it as a parent. \$\endgroup\$
    – Leggy7
    Commented Aug 11, 2016 at 10:07

1 Answer 1


Setting parent should be like:


First error is because of not set parent with a transform.

Second error is because of not using with the instantiated Transform.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .