- Home /
transform.SetParent(null); Not Working
I am trying to detach a game object from its parent, and this transform.SetParent(null); is just not working. this transform.parent = null; does not work as well.
Here is the part of the code:
public void Detach()
{
transform.SetParent(null);
playerScript.hasGun = false;
}
public virtual void Throw(float throwPower)
{
Detach();
gunRigidbody.velocity = Vector3.forward * throwPower + Vector3.up * throwPower / 10;
}
It should work https://docs.unity3d.com/ScriptReference/Transform.SetParent.html. Is Throw
being called?
Answer by Bunny83 · Feb 17, 2020 at 02:42 AM
We don't know where and how you call your method. So have you actually debugged if and when the method(s) are called by placing a Debug.Log statement in those methods?
My guess is that either:
you actually have a subclass of your gun / weapon class which overrides your "Throw" method and the overridden method does not call the base method. Therefore your original code got replaced. The base class code is not executed automatically. In an overridden method you have to call
base.Throw(throwPower);
inside the overridden method.or you might have some additional logic for picking up weapons automatically wheh you pump into them. When you detach the weapon it becomes a seperate object which could immediately collide with your player and might be re-attached immediately.
or the object is actually detached but somehow stuck at its current position due to collisions. Maybe the rigidbody of the weapon is kinematic? Does it actually have its own rigidbody and a collider?
lastly are you sure that you call your methods on the right object? A very common mistake is that people try to call method on prefabs instead of the instance they have created in the scene. Note that calling method on prefabs is possible, however changes to the prefab asset in your project should usually cause a warning or error in the editor and of course do not affect instantiated clones in the scene.
You just said "Not Working". That's not very specific. From the code we can see that you want to detach the weapon and make it accelerate into the worldspace forward direction and slightly upwards. It generally seems to be a bit strange that you hardcode the direction vector into the Throw method and just pass a power float value. Why not pass a Vector3 value which would make much more sense and is way more flexible for any kind of throwing action you might want to do with a weapon. Actually this should probably be a seperate component which you call Throwable. So it could be attached to any kind of object you might carry which you can throw.
All we can say is that SetParent(null); does work as it should when it's used properly
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a Interactive Computer Screen in Unity 5.5? 1 Answer
Need help in the stone throwing mechanic with aiming for mobile in Unity3d 1 Answer
How do i Instantiate gameObjects in between multiple points? 2 Answers
How do i get the LocalSize of an object? 2 Answers