How can I add a component (A Prefab) to a script after I instantiate it?

I have my prefab with the Bullet slot filled:

But upon being instantiated, it looks like this:

Here is my instantiation script:

using UnityEngine;

public class gunShooter : MonoBehaviour
{
    public float speed = 20f;
    public new Object bullet;
    public new Transform boxTransform;
    public Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        boxTransform = GetComponent<Transform>();
    }

    void Update()
    {
        transform.position = boxTransform.position;

        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody instantiatedProjectile = Instantiate(bullet, new Vector3(transform.position.x, transform.position.y,
                (float)(transform.position.z + 0.3)), transform.rotation) as Rigidbody;
            
        }
    }
}

Thank you for trying to help, I really appreciate anything you guys can tell me.

These make no sense:

public new Object bullet;
public new Transform boxTransform;

It should look like this:

public GameObject bullet;
public Transform boxTransform;

// as bullet is set in inspector, so no Get needed
// not sure what that transform is?

But your call to get transform boxTransform makes no sense. By calling GetComponent<Transform>() you are basically getting the position, rotation, and scale of the bullet. Which you don’t need to do as simply calling transform.position does the same thing.

So I would get rid of everything to do with boxTransform, and setup the Gameobject call for bullet the way I wrote, and all should be well. If of course your physics call is properly set right :slight_smile: