All camera compoents being switched off after setting its parent to null

When a client connects to my server, they grab the camera from the scene, which is in a default spectator view, and attach it to their character. I’m setting the parent of teh camera as so. When that clients disconnects, I try to detatch only the camera by setting its parent to null in OnDestroy() but it shows in the inspector with all its components switched off.

Are there asynchronous engine shenanigans happening where the camera object is being “partially” destroyed when the script logic finally sets its null parent?

What’s a better way to handle this?

public override void OnStartLocalPlayer(){
      base.OnStartLocalPlayer();
      cam = GameObject.Find("Camera");

      camSpecPos = cam.transform.position;
      camSpecRot = cam.transform.rotation;

      cam.transform.parent = transform;

      cam.transform.localPosition = new Vector3(0, 1.5f, -1.35f);
      cam.transform.localRotation = Quaternion.Euler(25, 0, 0);
   }

void OnDestroy(){
      cam.transform.parent = null;
      cam.transform.position = this.camSpecPos;
      cam.transform.rotation = this.camSpecRot;
   }

Also how do I format code here? ``` doesn’t work.

To format code use code tags, surrounded with or click the code option on the bar.

May be the solution you need.

Where is this code option ypu speak of? I can’t find it.

Same results using Transform::smile:eatchChildren(). In the script reference the detahment is happening before a Destroy is called. Where and how would I insert my code to run before networking calls Destroy from a disconnect?

Also I can’t use DetachChildren because it will detach other children that should be destroyed with the parent.

To the left of the “undo” Icon.

My apologies.

cam.transform.parent = null

…should work as per your implementation.

Are you disabling anything else upon destroying?

This might be worth looking at.

http://answers.unity3d.com/questions/320024/how-to-have-child-object-remain-after-parent-is-de.html

I’m not disabling anything. The engine just decides to disable my camera’s components when I unparent it in the OnDestroy call. The code above is all that affects the camera.

Before connecting to the server.

During connection and gameplay.

When I click Stop

I duct-taped it in the mean time with this.

Behaviour[] comps = cam.GetComponents<Behaviour>();
      foreach(Behaviour comp in comps){
         comp.enabled = true;
      }

What I have noticed from your screenshots is that your camera is untagged. Try changing the tag to “Main Camera”.