[SOLVED] How to set transform.parent via RPC?

Hey, all,

I’d really like to set the parent attribute of an object via an RPC call. Normal state synchronization of a transform property doesn’t seem to be working, so I’d like to do a one-shot RPC. Unfortunately, RPC calls can’t take Transforms as one of the parameters. Any suggestions?

I’ve done this, but I’m at home right now. I’ll post some snippets once I’m at the office, couple hours from now.

sending the parent would be useless as it won’t exist on other machines. you must use tags / names to identify parents and relink them again, not memory references.

That’s what I’m trying to do, communicate the equivalent object over the network. Rather than parent a client object to an object on the server, I want to tell the client to parent an object to the equivalent object that’s been Network.Instantiate(d).

Basically, like this:

@ RPC
function SetParent(id : NetworkViewID){
   var view : NetworkView = networkView.Find(id);
	myTrans.parent = view.GetComponent(Transform);
}

In another script:

var customizedPart = Network.Instantiate(partPrefab, Vector3.zero,Quaternion.identity,0); 
customizedPart.networkView.RPC("SetParent",RPCMode.All,networkView.viewID);

Except

Is that clearer?it doesn’t work. And really it should :stuck_out_tongue:

Sorry, completely forgot to post the code for this.

	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 );
		}
	}

In this instance, the object that I want to parent over the network has already been parented. The root of the parent object contains a network view that I’ll need to find on the other system. Obviously the object that I’m parenting also has a network view, since it’s making the RPC call to it’s other versions of itself.

So once you’ve parented the child network object to the other network object on the ‘owner’, you simply call SyncMount, and all the other child objects will automagically mount to their parent object.

Thanks, factoid, I think I just didn’t understand the NeworkView.Find() function well enough.

Hi! I have some problems using that code

1- In GetMountPath (), since the parent of the object i want to parent doesn’t have any parent, I changed this line:
Transform cur = transform.parent;
to this
Transform cur = transform;
otherwise this: while( cur.parent != null ) will always be null

2- I would like to know how should mountPath looks like because i keep getting a null in mountPoint in DoMount()

In the game i have the players objects called ‘Soul’ which are parents of the objects ‘piedras’
GetMountPath returns ‘piedras(Clone)’

I know this is a very old post but it’s the only one i found, if there is a better or simpler method to parent over a network i would be very happy to know it, thanks!