Instantiate not returning anything?

According to Object.Instantiate, the Instantiate function is supposed to return something but no matter what I use to catch it, it is always null. I’m attempting to instantiate a prefab, it actually instantiates the prefab but when I check to see if the object I stored it in is null, it always is. Here is the code:

public Transform bullet;

void Update() {
    if(Input.GetButtonDown("Fire1")) {
        GameObject newBullet;
        newBullet = (GameObject)Instantiate(bullet, transform.position, transform.rotation);

        if (newBullet) {
            Debug.Log("Object Returned");
        } else {
            Debug.Log("Nothing Returned");
        }
    }
}

I always get the null response, even tried safe casting and that still always returned null. Also tried getting the actual script that I was looking for (in this case it is a Projectile script) and that didn’t work either.

Edit: I originally listed bullet as a GameObject when it was in fact a Transform. Had I only made that mistake in my actual code, this question probably wouldn’t exist.

I ended up solving my own problem.

If you want to instantiate a prefab and use any scripts on it, keep in mind that the reference to it has to be a GameObject and not a Transform. For whatever reason, the Instantiate function will create the object if it is a Transform, but will not return anything in this case. Switching the variable to a GameObject allowed me to get a non-null return.

To add to this, when attempting to access a script on the instance you just created, there are 2 ways that you can do this. The first is to designate the prefab as a GameObject and capture it as such:

public GameObject object;

void Update() {
    GameObject newInstance = Instantiate(object) as GameObject;

    Projectile p = newInstance.GetComponent(typeof(Projectile)) as Projectile;
    p.Launch();
}

This is useful if you need to make calls to more than 1 script at a time. If, however, you only need to access 1 script, you can also take the shortcut of simply calling the object you want to instantiate whatever that script is named:

public Projectile object;

void Update() {
    Projectile p = Instantiate(object) as Projectile;
    p.Launch();
}

Doing this will also prevent you from accidentally trying to instantiate something that does not have a script you need on it.

I wonder if the caste (GameObject) is turning it null? Try declaring bullet as just as Object, no caste, and see if it’s still null then.

I’ve always used the recommended method: Instantiate(...) as Whatever;.

If your code was broke it wouldn’t get to the debug statements take the debug out of a if statement and just a simply print(“Bullit made”); statement would confirm your prefab showed.

another way to check it is to set up a code to check the scene if Bullet prefab exist if so return.

If your code was wrong Make sure to wrap any models in an empty game object. The size, position, and orientation need to be correct under the root gameobject. When you instantiate a gameobject under a parent you need to be sure to zero-out the localPosition, and localEulerAngles (set them = Vector3.zero). You need to also set the localScale = Vector3.one.