set parent of instantiated weapon over network.

I’ve got a little demo set up using unity’s masterserver while I learn to script and my recent problem is i’m trying to instantiate a weapon over the network and set it to parent a character hand and have it show its parented to other connected players. It work’s in single player but I realize that I need to get the network id + other things for the player and use that with rpc calls to do it right.

My first attempt at this without knowing.
What it currently does is spawn the object correctly for the player but for other players, it spawns at 0,0,0 and is not parented.

var rightHandWeapon : Transform; // weapon holder
var Short_Sword : GameObject;


function Start () {

}

function Update () {
	if (Input.GetKeyDown(KeyCode.P)){
    	    StartCoroutine("spawnsword");

	}
}

    @RPC
    function spawnsword(){
    	var weapon = Instantiate(Short_Sword,rightHandWeapon.position,rightHandWeapon.rotation);
    	weapon.transform.parent = rightHandWeapon;
    }

I found an example that looks very promising though its in C# and I can’t fully convert to js. I realize whats going on mostly and i can see its pretty straightforward but my lack of scripting knowledge is showing. So, I was wondering if there’s any place that sort of explains the process to learn from that would be great. I’ll continue researching the code from the example I found below.

string GetMountPath()

    {

        string str = "";

        Transform cur = transform.parent;

        while( cur.parent != null )

        {

            str = cur.name + "/" + str;

            cur = cur.parent;

        }

        return str.Trim( "/".ToCharArray() );

    }

   

    [RPC]

    void RPCLinkToParent( string mountPath, NetworkViewID rootID, Vector3 lPos, Vector3 lEuler )

    {

        Transform rootPoint = NetworkView.Find( rootID ).transform;

        //Debug.Log( "Got SyncMount for " + mountPath + " on object " + rootPoint.gameObject.name );

        StartCoroutine( DoMount( mountPath, rootPoint, lPos, lEuler ) );

    }

   

    IEnumerator DoMount( string mountPath, Transform rootPoint, Vector3 lPos, Vector3 lEuler )

    {

        Transform mountPoint = rootPoint.transform.Find( mountPath );

        while( mountPoint == null )

        {

            //Debug.Log( "Mount point didn't exist yet" );

            yield return new WaitForSeconds( 0.5f );

            mountPoint = rootPoint.transform.Find( mountPath );

        }

 

        //Debug.Log( "Mounting object " + gameObject.name + " to position " + lPos );

        transform.parent = mountPoint;

        transform.localEulerAngles = lEuler;

        transform.localPosition = lPos;

    }

   

    void SyncMount()

    {

        if( networkView.isMine )

        {

            //Debug.Log( "Sending SyncMount for " + gameObject.name, gameObject );

            string mountPath = GetMountPath();

            NetworkViewID vID = transform.root.networkView.viewID;

            //Debug.Log( "Root path is " + mountPath + ", root ID is " + vID );

            networkView.RPC( "RPCLinkToParent", RPCMode.OthersBuffered, mountPath, vID, transform.localPosition, transform.localEulerAngles );

        }

    }

Seems to be solved now.

var rightHandWeapon : Transform; // drag the gun bone here
//var weapon : Transform;
var Short_Sword : GameObject;


function Start () {

}

function Update () {
	if (Input.GetKeyDown(KeyCode.P)){
	var viewID : NetworkViewID= Network.AllocateViewID();
	networkView.RPC("spawnsword",
					RPCMode.AllBuffered, 
							viewID, 
							transform.position);	
    	//rightHandWeapon = Instantiate (weaponPrefab,rightHandWeapon,transform.rotation);


	}
}

    @RPC
    function spawnsword (viewID : NetworkViewID, location : Vector3) {
    
    	var weapon = Instantiate(Short_Sword,rightHandWeapon.position,rightHandWeapon.rotation);
    	weapon.transform.parent = rightHandWeapon;
    	
    	var nView : NetworkView;
		nView = weapon.GetComponent(NetworkView);
		nView.viewID = viewID;
    }