- Home /
Network.Instantiate parenting on other clients
Hi all, I need help to parenting an object instantiaded via Networking I've an object called SpellGenerator attached to players. When i press a button i would like to cast a spell that play a particle (a magic shield) and update the player's stats. I have created an object Named FireShield with the script attached for play the particle. The problem is that when i hit the button the object is instantiated as child of SpellGenerator correctly only in the device i'm instatiating. In others the spell are instatiated but in position 0 0 0 in world space. How can i parenting the spell to attach to the correct player? here the code:
public void CastSpell(SpellType spellType)
{
switch (spellType)
{
case SpellType.Shield:
Debug.Log("Calling RPC_CastCircleSpell");
//networkView.RPC("RPC_Cast", RPCMode.All);
GameObject test = (GameObject)Network.Instantiate(circleSpell, transform.position, Quaternion.identity, 0);
//this works locally
test.transform.parent = transform;
break;
case SpellType.ShortShot:
break;
default:
break;
}
}
public class FireShield : MonoBehaviour, IBuff
void OnNetworkInstantiate(NetworkMessageInfo info)
{
//HOW CAN I ATTACH FIRESHIELD TO THE CORRECT SpellGenerator and apply buff to the correct player?
BuffDurationTimer = BaseBuffDuration;
Transform particle = gameObject.transform.FindChild("Dark_Main");
particle.particleSystem.Play();
}
there is a better way to do what i am trying to do? thank you for help
Edit: SpellGenerator an FireShield have NetworkView attacched
Answer by taribo · Jul 19, 2014 at 09:13 PM
SOLUTION:
public class SpellGenerator : MonoBehaviour {
...
public void CastSpell(SpellType spellType)
{
...
GameObject spell = (GameObject)Network.Instantiate(circleSpell, transform.position, Quaternion.identity, 0);
spell.GetComponent<FireShield>().Cast(networkView.viewID);
//local attaching to parent
spell.transform.parent = transform;
...
public class FireShield : MonoBehaviour, IBuff
{
...
public void Cast(NetworkViewID callerID)
{
networkView.RPC("RPC_Cast", RPCMode.All, callerID);
}
...
[RPC]
void RPC_Cast(NetworkViewID callerID)
{
//recupero l'oggetto a cui devo attacare il buff (SpellGenerator) a partire dal callerID che ho passato
NetworkView view = NetworkView.Find(callerID);
GameObject spellGenerator = view.gameObject;
//attacco la spell allo SpellGenerator
transform.parent = spellGenerator.transform;
BuffDurationTimer = BaseBuffDuration;
Transform particle = gameObject.transform.FindChild("Dark_Main");
particle.particleSystem.Play();
}
this work quite well
Thanks to your solution, I could take the right way to implement my game, after several hours.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154454im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullReferenceException with SwitchController script 1 Answer
In unity3d client “push” an object with a rigidbody2d slower than the host 0 Answers
Network Tutorial Interpolation - TPS Auth 0 Answers
[Photon] How many transforms can be serialized? 1 Answer
Moving a scenery piece from a client 0 Answers