# How to set parent for prefab in Unity

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;
btn.SetParent(_canvos);
}


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??

• 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(). – Leggy7 Aug 11 '16 at 9:50
• @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 – mmoj Aug 11 '16 at 9:59
• 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. – Leggy7 Aug 11 '16 at 10:07

InTank.SetParent(_canvos.transform);