How can I sent a gameobject from the player controller to another gameobject by scripting?

I write two scripts for my player to collect a power up(gameobject) and use it to fire from a blaster(gameobject) to shoot. I tried to call one script into another one but I don’t know if it’s possible. How can I grab a gameobject in game and put it into the blaster for use? The first script is the player attaining the power up and the second first is the Projectile of the blaster.

Why not just add in a GameObject variable in the shooting script?

The question seems to be what you mean by sending a gameobject to another. I can think of two ways:

  • Have an array of GameObjects for that purpose in your character and when picking up the projectile you could set an empty entry of the array to it so you are able to address it for further use. You might need to disable the collision and renderer of it so it “looks like picked up”. You could also use a generic List to add and remove projectiles from it in the character when picked up and fired off.
    The problem here would be that you will have to differentiate between available projectiles and those you will have to instantiate because there are none left in either array or list.
    You could even have a manager which holds all the projectiles there can possibly be at start so you won’t have to instantiate them on runtime.

  • A second approach could be just an integer of ammunition in your character. When picked up destroy the projectile and increment the int. Then you can instantiate a new one and decrement the int when you fire. If the game is no that big I would try this approach because it takes better care of cleaning up by itself.

Just another hint, for your tag-checks. Try using this, it’s shorter:
hit.gameObject.CompareTag(“Grass Ball Shot”)