Saving and applying an array of Vector3 velocities on an array of GameObjects

Hi all, I’m working on a VR asteroid field, where I pause and unpause using a 3D button that pushes via timescale; so I’m trying to freeze all of my objects when the user hits the pause button, and returns their velocities when the user pushes the same button. I already have the target objects in an array, but I’m struggling to save and return each objects personal velocities.

The error I am getting referencing the first line that the perPauseVel array appears in the PauseAsteroids Function:

NullReferenceException: Object reference not set to an instance of an object
 GameControllerScript.PauseAsteroids (System.Boolean paused) (at Assets/Scripts/GameControllerScript.cs:258)

I’m not sure how to solve that, but here is the code I am running:

     private Vector3[] prePauseVel;
     private Vector3[] prePauseAngleVel;
    ...
     private void PauseAsteroids(bool paused)
     {
         //get Array of all asteroids
         GameObject[] allAsteroids = GameObject.FindGameObjectsWithTag("asteroid");
         //loop through Asteroids to apply pause-unpause actions to each
         for (var i = 0; i < allAsteroids.Length; i++)
         {
             if (paused)
             {
                 //save Vector3 velocities to two arrays of prePause Velocities
                 prePauseVel _= allAsteroids*.GetComponent<Rigidbody>().velocity;*_

prePauseAngleVel = allAsteroids*.GetComponent().angularVelocity;*
//freeze Asteroids in position
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.FreezeAll;*
}
else if (!paused)
{
//unfreeze Asteroids
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.None;*
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.FreezePositionY;*
//reapply force and torque of their prePause velocities from saved arrays
allAsteroids_.GetComponent().AddForce(prePauseVel*,ForceMode.VelocityChange);
allAsteroids.GetComponent().AddTorque(prePauseAngleVel, ForceMode.VelocityChange);*_

}
}
}
I’m pretty new to Unity and C#, but I’m not sure how I should reference my perPauseVel arrays when the asteroids are constantly changing in number and velocities. The code worked when it was a single asteroid and these weren’t arrays… but I’m not sure how I should change it to make it work with multiple asteroids.

I found the key to the solution while watching a video from Unity3D College… set a new blank vector 3.

After I saw that, I just needed to set their reference to a new vector 3 at the beginning, and then if loop their length on pause before populating it. Now it pauses and resumes across all asteroids just the way I wanted, and refreshes the list on each new pause.

    private Vector3[] prePauseVel = new Vector3[0];
    private Vector3[] prePauseAngleVel = new Vector3[0];

    ...

    private void PauseAsteroids(bool paused)
    {
        //get Array of all asteroids
        GameObject[] allAsteroids = GameObject.FindGameObjectsWithTag("asteroid");

        if (paused)
        {
            //create blank arrays for storage on each new pause
            prePauseVel = new Vector3[allAsteroids.Length];
            prePauseAngleVel = new Vector3[allAsteroids.Length];
        }

        for (var i = 0; i < allAsteroids.Length; i++)
        {
            if (paused)
            {
                //save Vector3 velocities to an array the prePause Velocities
                prePauseVel _= allAsteroids*.GetComponent<Rigidbody>().velocity;*_

prePauseAngleVel = allAsteroids*.GetComponent().angularVelocity;*
//freeze Asteroids in position
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.FreezeAll;*
}
else if (!paused)
{
//unfreeze Asteroids
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.None;*
allAsteroids*.GetComponent().constraints = RigidbodyConstraints.FreezePositionY;*
//apply force and torque of their prePause velocities from saved array
allAsteroids_.GetComponent().AddForce(prePauseVel*,ForceMode.VelocityChange);
allAsteroids.GetComponent().AddTorque(prePauseAngleVel, ForceMode.VelocityChange);*_

}
}
}