get score script from the object that instantiated the missile?

Hi,

In my game a player can shoot a missile by instantiating a missile prefab that takes off on awake. The missile isn’t the child object of the player instantiating it because I can’t have the missile transform be linked to the players transform, it causes the missile to fly around the player and not in a straight line in the direction that the player is shooting.

My question is, how do I get acces to the player who instantiated the missile’s score script, from the instantiated missile prefab? I can’t add it in the inspector, since it’s a different scorescript depending on who’s shooting the missile and there’s more than 1 player. I can’t set it to be a child object as I explained above, so is there a good way to do this that anybody can tell me? Thank you in advance, your help is greatly appreciated

If you really want to do it this way, some things you need to remember:

  • after instantiating every prefab is just an ordinary game object
  • if you write a script or put in a script (WantToUseThePlayer down below) on the prefab where you have a public Player (or whatever you call your Player script you need), then you can set that value after instantiating
    mockup example:
// [...]
var missile = Instantiate(missilePrefab);
// set the stuff for the instantiation, rotation, location, etc
missile.GetComponent<WantToUseThePlayer>().player = this;
// [...]
2 Likes

Thank you, I’ll try that