Instantiated Prefab's GameObjects not all showing in Game.

This code below instantiates a prefab that has a sprite, with a 2D light as a child. In the editor, when I manually drag in one of these prefabs, everything works fine. But when the prefab is spawned via code in-game, the prefab is spawned, as confirmed by a Debug.Log, but only the child light is visible, not the parent sprite.

In the image below, the left is when I drag in a prefab. On the Right is the light working, but the sprite not visible. I was thinking this issue might be layers/sorting, but If it all works when dragging it in, would that mean the issue lays elsewhere?

Any help is appreciated!

private void PlayerShoot()
    {
        GameObject  ChosenMuzzleFlash = muzzleFlashArray[Random.Range(0, muzzleFlashArray.Length)];
    
        Quaternion newRotation = Quaternion.LookRotation(muzzleFlashPoint.transform.position, Vector3.up);
        var muzzleFlashInstance = Instantiate(ChosenMuzzleFlash, muzzleFlashPoint.transform.position, newRotation);
        Debug.Log("Terst" + muzzleFlashInstance.name);
    }

Did you open the inspector and try finding the instantiated object? Check for attributes like the scale and make sure that they are correct.

The sprite could be hiden by something indeed. Create a new Vector for your position with a bigger z

1 Answer

1

Thanks for the comments, after checking into the inspector, I found that everything was set to 0 on the Z for the position. After changing the Z to 1, I was able to see sprite in game!

I was able to simplify code as well;

private void PlayerShoot()
{
        GameObject  ChosenMuzzleFlash = muzzleFlashArray[Random.Range(0, 
        muzzleFlashArray.Length)];
        muzzleFlashInstance = Instantiate(ChosenMuzzleFlash, muzzleFlashPoint.transform.position, 
        muzzleFlashPoint.transform.rotation);
}