1
votes

How can I find / memorize in Unity(2d) multiplayer the player who instantiated a given object? For example if I have a shooting game and players can instantiate bullets and I want to find out a bullet owner, how can I do it? Thanks

p.s. For multiplayer server I use UnityEngine.Networking (not Photon).

1
if the script was on the player, and you instantiated the items there, the player would be the parent. So, you could just check who the parent isBugFinder
@Vlad6001 Would you mind posting code that you already have?Eliasar

1 Answers

1
votes

Assuming you have a component script on your bullet named Bullet and a variable to hold the parent/creator as GameObject creator, you need to get the component script on the instantiated bullet and set the variable to whoever created it.

void CreateBullet(GameObject bulletPrefab, Vector3 position, Vector3 rotation)
{
    GameObject bullet = Instantiate(bulletPrefab, position, rotation);
    bullet.GetComponent<Bullet>().creator = gameObject;
}