0
\$\begingroup\$

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

\$\endgroup\$
  • 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 Aug 11 '16 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 Aug 11 '16 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 Aug 11 '16 at 10:07
1
\$\begingroup\$

Setting parent should be like:

InTank.SetParent(_canvos.transform);

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

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

\$\endgroup\$

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.