Something went wrong with Instantiate

I used an instantiate code to fire projectiles in a first person shooter, using the camera as a basis to fire from. It’s worked perfectly until recently. Now, it defaults to firing a good foot or two above the player’s actual viewpoint and, if the player is moving while firing, it seems to fire ahead of the direction of the player, so steering left/right when the player is, spawning in the player’s face and disappearing when the player is backing up, and going more or less the way it originally fired when the player is actively going forward. I don’t know what caused it to suddenly break but here’s the code:

            GameObject synapsepearl = (GameObject)Instantiate(pearl_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
            synapsepearl.GetComponent<Rigidbody>().AddForce( cam.transform.forward * pearlImpulse, ForceMode.Impulse);

Could you have more than one camera in your scene?

Just the one, unless you count the “Death Cam” that instantiates to replace the main cam upon death but it’s not actively in the scene. The projectile still spawns from the player but not directly in front of it anymore.

Could the spawning pearl be colliding with the player’s collider?

Also, try and think about what changes you made to your project, and camera setup, since it last worked.
I tried your instantiate code on Unity’s RigidbodyFirstPersonController, and everything works fine.

It can’t be a problem with the player’s collider because the pearl destroys itself when it collides with anything. I’ll try making a new camera from scratch and just giving it the same scripts and position.

Update: Guess I should have tried this before I posted a thread. I made a new one, pasted over the transform and reapplied the cursor script, that fixed it. Don’t know what caused it to go wrong but at least I was able to fix it fairly quickly.

1 Like

You fixed it, that’s what counts! :slight_smile:

Unfortunately, it seemed to only be temporary as it’s acting up yet again. I even attempted to make an empty for it to spawn from so that I wouldn’t have to do the whole cam forward thing addition but even that didn’t help.

The code is:

            GameObject synapsepearl = (GameObject)Instantiate(pearl_prefab, shot.transform.position, shot.transform.rotation);
            synapsepearl.GetComponent<Rigidbody>().AddForce( shot.transform.forward * pearlImpulse, ForceMode.Impulse);

You can clearly see, in this image, where the Empty is in the 3D view above, yet it deliberately ignores that and fires the pearls above the player’s head, and of course it has problems spawning elsewhere if you move while shooting:

Even if I thought going through the trouble of deleting the entire player and doing it over again would fix it, I don’t want it to keep breaking every single time I change a scene.

Update: Apparently, it varies from scene to scene because dragging in another scene’s as a prefab seemed to “fix” it but I’m not sure if it’s just going to keep randomly breaking like this.

Do you have the Tool handle set to Pivot, or Center?


If it is set to center, change it back to Pivot to see where the actual position is.

The problem reared its ugly head again. This time, changing cameras doesn’t even help. As you can see, it’s Pivot and the pearl is clearly spawning wherever it wants to rather than where the camera actually is… You can even see exactly where it spawned from because of the temporary audio object in the spot it shot out from.

I found a solution but I’m honestly not sure why it worked. I changed the weapon firing to an IEnumerator coroutine and, somehow, “yield return null;” seemed to have completely fixed it. Hopefully, it’s actually fixed for good this time. If so, here’s the solution for future reference. I didn’t even have to change the camera, the fix carried over between scenes.