So with below code, I am trying to make an instantiated object the child of main.camera so it looks as if the person activating it is “holding it”
using UnityEngine;
using System.Collections;
public class Haxxor3000Behaviour : MonoBehaviour {
private GameObject haxxorObject;
private Transform cameraTransform;
void SwitchState (bool setActive) {
cameraTransform = Camera.main.transform;
if(setActive){
haxxorObject = Instantiate(this, cameraTransform.position + cameraTransform.forward * 2, cameraTransform.rotation) as GameObject;
this.transform.parent = cameraTransform;
} else{
Destroy(this);
}
}
// Update is called once per frame
void Update () {
}
}
However this results in the following error:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
I think this happens because main.camera is itself a child of the prefab player.
Is there any way to fix this? or maybe another solution to this problem I am not seeing?
All help is appreciated,
cheers Nopogo