GameObject child an other GameObject with code

hi all
my game is fps shooter and i create full body controller
i have 1 gameobject i called GunPlace (on right hand)
and i have guns
i want when press “F” button , gun child of GamePlace and i can gun Transform change (i can set Transform and rotation for fix place)

but no idea for this
plz help me

parenting objects: Unity - Scripting API: Transform.parent

i create a code but dont work :frowning:

#pragma strict
//attach to gunPlace


public var Gun : Transform;	

function Update ()
{
 var dist = Vector3.Distance(Gun.transform.position , this.transform.position );
	if(dist <= 1)
	{
			if(Input.GetButtonDown("Jump"))
			{
			
			Gun.parent = transform ;
			Gun.localPosition = this.transform.position;
			
			}
				
	}

}

doesn’t work in what sense? errors (in case what’s the error?) does something other than what you want (what does it do?) etc.

only thing that jumps out is this line:

Gun.localPosition = this.transform.position;

… have a look at the ref page for localPosition: Unity - Scripting API: Transform.localPosition

give me this error :
UnassignedReferenceException: The variable Gun of ‘GunPlace’ has not been assigned.
You probably need to assign the Gun variable of the GunPlace script in the inspector.
GunPlace.Update () (at Assets/scripts/GunPlace.js:9)

and fixed but

I do not understand Gun.parent , Gun child and gunPlace is parent or gunPlace is child and gun is parent ?

cuz fixed with code and gun attach to gunplade (maybe unlike ) because gun are bad place

Gun.parent = transform;

the property “parent” of “Gun” is set to the transform the script is attached to:

The gun is the child of gunPlace (which has the above code attached to it).

from the localPosition reference page

// Move the object to the same position as the parent:
    transform.localPosition = Vector3(0, 0, 0);

“this.transform.position” is the offset of gunPlace from the center of the world, “transform.localPosition” is the offset of gunPlace from it’s parent object. If you use either of these as the Gun’s localPosition you’re going to end up with the gun being far from the gunPlace position.

i don’t know , why this don’t work !

and this erro :
UnassignedReferenceException: The variable Gun of ‘GunPlace’ has not been assigned.
You probably need to assign the Gun variable of the GunPlace script in the inspector.
GunPlace.Update () (at Assets/scripts/GunPlace.js:9)

but i attach gun in inspector :frowning:

I think that might be from:

public var Gun : Transform; 

 ... Gun.transform.position ...

transform.transform.position isn’t going to work (transform doesn’t have a transform property)… try just Gun.position.

i found a tutorial and change i code to mecanim

orginal code is :

i set 2 bool parameter
1 Pistol
2 Ak

and change code to :

change gun is work but setbool not work !

and give me a error :

how can i do it work ?!