Referencing a script from another script?


Hello, I’ve been working on a multiplayer game recently , i’m starting to implement damage and health system but i’m facing a small issue, what i’m doing is i’m giving each gun in my player inventory a damage int, so in order to use this damage variable i’m referencing the gun using Gameobject.findObjectOfType(gun).getComponent<gun> on the bullet prefab everything works fine, until the other player joins, the script will find 2 or multiple gameObjects of type gun which will mix up the variables, for example one player will be holding a pistol with 20 Damage while the other player holding a shotgun with 50 damage the script will be confused as there is 2 gun scrips with different variables

1-note that I’m instantiating the bullet so i’t can’t be assigned directly in the inspector

2-I don’t want to use raycasts, i’m good with projectiles

if you managed to solve this, thank you, you’re a genius and you saved me hours of work.


  • Alejandrazo
    edited July 2022

    Hey guy,¿can you set child of the player the gun?

    If yes, you can do the next




    0* Index position of the Gun inside the parent(player)

    If you cant do that, you can ask me and then tell me why didnt work for better understand the problem.

  • Hi, you should not find the gameobject that way, because it will find the objects in the current scene (of the other players as well). You should store a reference to the local player and then get the gun component of that gun.