- Home /
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[i] = allAsteroids[i].GetComponent<Rigidbody>().velocity;
prePauseAngleVel[i] = allAsteroids[i].GetComponent<Rigidbody>().angularVelocity;
//freeze Asteroids in position
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
else if (!paused)
{
//unfreeze Asteroids
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
//reapply force and torque of their prePause velocities from saved arrays
allAsteroids[i].GetComponent<Rigidbody>().AddForce(prePauseVel[i],ForceMode.VelocityChange);
allAsteroids[i].GetComponent<Rigidbody>().AddTorque(prePauseAngleVel[i], 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.
Always check if someone exists before trying to access it
if(gameObject.GetComponent<Component>() != null)
better yet, store it once
Component component = gameObject.GetComponent<Component>();
if(component != null)
How should I phrase this... Both arrays exist. $$anonymous$$y issue is that unlike the allAsteroids array, which has a pretty straight forward reference to prep it with, the issue is the null reference I'm getting for having not prepared the prePauseVel arrays because those arrays need to acquire their Vector3 entries from the allAsteroids at the time of pause, so they can be returned on a separate instance of the function once unpaused.
I'm trying to understand how I should refer the array of prePauseVel Vector3s to the array of allAsteroids velocities, while trying to populate the prePauseVel array with those returns.
Does that make sense?
Answer by hwkeyser · May 22, 2019 at 07:20 PM
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[i] = allAsteroids[i].GetComponent<Rigidbody>().velocity;
prePauseAngleVel[i] = allAsteroids[i].GetComponent<Rigidbody>().angularVelocity;
//freeze Asteroids in position
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
else if (!paused)
{
//unfreeze Asteroids
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
allAsteroids[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
//apply force and torque of their prePause velocities from saved array
allAsteroids[i].GetComponent<Rigidbody>().AddForce(prePauseVel[i],ForceMode.VelocityChange);
allAsteroids[i].GetComponent<Rigidbody>().AddTorque(prePauseAngleVel[i], ForceMode.VelocityChange);
}
}
}
Your answer
Follow this Question
Related Questions
I dont know why im getting a null reference exception 1 Answer
Looping through vector3 list slow 1 Answer
What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers
Object snapping to floor and other objects in VR 1 Answer
How do I place the matching Vector3 array values of a Vector3 array in a new array ? 1 Answer