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.