Transform.position of pooled object resetting to prefab value the frame after it spawns

Hi,

I’m having an issue where prefabs spawned in with an object pooler are resetting to their prefab position the frame after they spawn in as shown below. The kicker is this only happens at lower frame rates, I have to limit my FPS below 60 to reliable reproduce it. At higher frame rates it works as intended.

9644084--1371569--Pooler issue.png

Here is my code for the object pooler, nothing else sets a ball’s transform.position except here when the ball is spawned.

private void MultiBall(PlayerController player, GameObject mirrorObject)
{
    if (player.multiBallActive)
    {
        for (int i = 0; i < powerUpInfo.multiBallCount; i++)
        {
            // get a pooled ball object
            GameObject ballObject = ballPool.objectPool.Get();

            // Store rigidbody
            Rigidbody ballRb = ballObject.GetComponent<Rigidbody>();

            Debug.Log("Player " + player.playerID + " got a MultiBall. Ball spawned at " + ballObject.transform.position);

            // Assign the Player that is spawning the ball to newly spawned ball
            ballObject.GetComponent<PlayerTracker>().SetCurrentPlayer(player.gameObject);

            // assign it to the ball object in the hierarchy
            ballObject.transform.parent = ballContainer.transform;

            // spawn the balls above the player's block
            ballObject.transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 1, player.transform.position.z);

            Debug.Log("Player " + player.playerID + " set MultiBall position to " + ballObject.transform.position);

            // enable trail
            ballObject.GetComponent<TrailRenderer>().enabled = true;

            // Add to the active ball count
            player.activeBalls++;

            // Add ball to player's balls list
            player.balls.Add(ballObject);

            // get the ball's rigidbody and give it a vertical velocity with a randomised direction on the x axis. I have honestly no idea why the y axis needs to be a negative to go upwards \o/
            ballRb.velocity = new Vector3(Random.Range(-powerUpInfo.multiBallDirection, powerUpInfo.multiBallDirection), -1, 0).normalized * playerInfo.ballDefaultSpeed;

            Debug.Log("Player " + player.playerID + " MultiBall has a velocity of " + ballObject.GetComponent<Rigidbody>().velocity);
        }

        // If Mirror is active spawn balls from there too
        if (player.mirrorActive)
        {
            for (int i = 0; i < powerUpInfo.multiBallCount; i++)
            {
                // Get a pooled ball object
                GameObject ballObject = ballPool.objectPool.Get();

                // Store rigidbody
                Rigidbody ballRb = ballObject.GetComponent<Rigidbody>();

                Debug.Log("Player " + player.playerID + " got a MirrorBall. Ball spawned at " + ballObject.transform.position);

                // Assign the ball to the player
                ballObject.GetComponent<PlayerTracker>().SetCurrentPlayer(player.gameObject);

                // assign it to the ball object in the hierarchy
                ballObject.transform.parent = ballContainer.transform;

                // set start position just above the mirror's block
                ballObject.transform.position = new Vector3(mirrorObject.transform.position.x, mirrorObject.transform.position.y + 1, mirrorObject.transform.position.z);

                Debug.Log("Player " + player.playerID + " set MirrorBall position to " + ballObject.transform.position);

                // Enable trail
                ballObject.GetComponent<TrailRenderer>().enabled = true;

                // Add to the active ball count
                player.activeBalls++;

                // Add ball to player's balls list
                player.balls.Add(ballObject);

                // get the ball's rigidbody and give it a vertical velocity with a randomised direction on the x axis. I have honestly no idea why the y axis needs to be a negative to go upwards \o/
                ballRb.velocity = new Vector3(Random.Range(-powerUpInfo.multiBallDirection, powerUpInfo.multiBallDirection), -1, 0).normalized * playerInfo.ballDefaultSpeed;

                Debug.Log("Player " + player.playerID + " MirrorBall has a velocity of " + ballObject.GetComponent<Rigidbody>().velocity);
            }
        }

        player.multiBallActive = false;
    }
}

I found this thread with someone having the same problem as me unfortunately the fix that worked for them didn’t for me Reddit - Dive into anything

Anybody have any idea what might be causing this?

Move the Rigidbody, not the Transform.

1 Like

Thank you for the quick reply. I tried setting the rigidbody position instead but the same thing happened but in reverse, it spawned at the prefab position then warped to the correct position the next frame. So figured if I set the transform and the rigidbody position on spawn it wouldn’t warp around, and et voila it works.

// spawn the balls above the player's block
Vector3 spawnPos = new Vector3(player.transform.position.x, player.transform.position.y + 1, player.transform.position.z);
ballObject.transform.position = spawnPos;
ballRb.position = spawnPos;

Thanks again, I’ve been banging my head against the wall with this one.