Problem with script attached to instantiated object.

Hi everyone, hopefully someone can help me out here.

I currently have a script attached to an in game weapon, which I want the player to be able to pick up.
When I place the weapon in the world manually the script attached to it works perfectly, however when the weapon has been instantiated it does not work correctly.

public class weaponPickup : MonoBehaviour
{
    bool inRange;
    public GameObject player;

    private void Start()
    {

        player.GetComponent<Inventory>().pistolAmmo += 10;
        player.GetComponent<Inventory>().pistol = true;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        inRange = true;

    }


    private void OnTriggerExit2D(Collider2D collision)
    {
        inRange = false;
    }



    private void Update()
    {

       
        if (inRange)

        {

            if (Input.GetKeyDown("f"))
            {
                PickUp();
               
            }


           
        }
    }
    void PickUp()
    {
        player.GetComponent<Inventory>().pistolAmmo += 10;
        player.GetComponent<Inventory>().pistol = true;
        Destroy(gameObject);
     
    }

}

The GetComponent code is what is not working on the instantiated object.
Once again it works fine if i place the weapon in the world manually, just not when the object is instantiated. The destroy object code always works. If anyone could shed some light as to why this would be it would help me A LOT. Thanks lads.

Can you share the Instantiate code?

What about it is not working? Is there an error?

Sorry… I uploaded the wrong code originally, fixed it now check the original post again.

The thing that isnt working is that when I press “f” it IS destroying the gameObject but isnt doing this:

  • player.GetComponent().pistolAmmo += 10;
  • player.GetComponent().pistol = true;

so it isnt changing the value of the bool in my player inventory script and isnt adding 10 to the pistolAmmo float also in my player Inventory script.

here is the instantiate code:

public GameObject player;
public GameObject pistolPreFab;

    public void ClickedPistol()
    {
        if (currentMoney > 9)
        {
            player.GetComponent<Inventory>().money -= 10;
            currentMoney -= 10;

            pistol = Instantiate(pistolPreFab, pistolSpawn.position, pistolSpawn.rotation);

        }

    }

Thanks.

How are you assigning the player reference on your pistol script? I’m guessing you’re actually referencing the player prefab on your pistol and not the player in your Scene. Your Instantiate code doesn’t set the reference and it’s not possible for a prefab to reference an object from a scene. I think you need to add this code to your Instantiate code:

           pistol = Instantiate(pistolPreFab, pistolSpawn.position, pistolSpawn.rotation);
           weaponPickup wp = pistol.GetComponent<weaponPickup>();
           wp.player = player;

I think youre maybe right, had a friend on discord helping me and he had said something similar. Let me give it a go.
Thanks for your time btw mate.