- Home /
transform.parent = null; not working
so, i'm trying to make an object duplicate itself then have the original unparent from the hips and have the duplicate stay in place. It duplicates perfectly fine and parents the duplicate to the hips perfectly fine, however the original still moves with the hips despite "transform.parent = null" being in the code. Can anyone tell me what i am doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magazine : MonoBehaviour {
public int ammo = 6;
private bool duped = false;
public GameObject mag;
public Transform Hips;
// Use this for initialization
void Start () {
}
public int GetAmmo()
{
return ammo;
}
public bool GetDuped()
{
return duped;
}
public virtual void WasDuped(int ammos)
{
duped = true;
GameObject mag = Instantiate(Resources.Load("MagSpawner"), gameObject.transform.position, gameObject.transform.rotation) as GameObject;
ammo = ammos;
mag.transform.parent = Hips; //this works fine
transform.parent = null; // <------the line that does not work
}
public virtual void SetAmmo(int ammos, GameObject self)
{
ammo = ammos;
if (ammo <= 0) {
Destroy (self, 2f);
}
}
// Update is called once per frame
void Update () {
}
}
Have you tried to call the transform.SetParent(null)
ins$$anonymous$$d of the property ?
Any error in the console ? (make sure Errors are enabled) What does Debug.Log( transform.parent ) ;
outputs after you try to detach the child ?
Null UnityEngine.Debug:Log(Object) $$anonymous$$agazine:WasDuped(Int32) (at Assets/Guns/$$anonymous$$agazine.cs:33) VRT$$anonymous$$.GrabAttach$$anonymous$$echanics.$$anonymous$$agGrab:CreateJoint(GameObject) (at Assets/Guns/$$anonymous$$agGrab.cs:38) VRT$$anonymous$$.GrabAttach$$anonymous$$echanics.VRT$$anonymous$$_BaseJointGrabAttach:SnapObjectToGrabToController(GameObject) (at Assets/VRT$$anonymous$$/Scripts/Interactions/GrabAttach$$anonymous$$echanics/VRT$$anonymous$$_BaseJointGrabAttach.cs:128) VRT$$anonymous$$.GrabAttach$$anonymous$$echanics.VRT$$anonymous$$_BaseJointGrabAttach:StartGrab(GameObject, GameObject, Rigidbody) (at Assets/VRT$$anonymous$$/Scripts/Interactions/GrabAttach$$anonymous$$echanics/VRT$$anonymous$$_BaseJointGrabAttach.cs:43) VRT$$anonymous$$.VRT$$anonymous$$_InteractGrab:IsValidGrabAttempt(GameObject) (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_InteractGrab.cs:381) VRT$$anonymous$$.VRT$$anonymous$$_InteractGrab:PerformGrabAttempt(GameObject) (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_InteractGrab.cs:366) VRT$$anonymous$$.VRT$$anonymous$$_InteractGrab:AttemptGrabObject() (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_InteractGrab.cs:355) VRT$$anonymous$$.VRT$$anonymous$$_InteractGrab:DoGrabObject(Object, ControllerInteractionEventArgs) (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_InteractGrab.cs:402) VRT$$anonymous$$.VRT$$anonymous$$_ControllerEvents:OnAliasGrabOn(ControllerInteractionEventArgs) (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_ControllerEvents.cs:741) VRT$$anonymous$$.VRT$$anonymous$$_ControllerEvents:EmitAlias(ButtonAlias, Boolean, Single, Boolean&) (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_ControllerEvents.cs:1695) VRT$$anonymous$$.VRT$$anonymous$$_ControllerEvents:Update() (at Assets/VRT$$anonymous$$/Scripts/Interactions/VRT$$anonymous$$_ControllerEvents.cs:1100)