So I am working on a gravity simulation in 3D, and I have been getting some really strange NullReferenceExceptions. The errors for now are occurring on line 107, it reads:
tSeparationVector = objects[j].transform.position - objects*.transform.position;*
Here is the rest of the code:
var objectList = new Array();
private var objects : GameObject[] = new GameObject[10];
var masses = new Array();var locX:int; var locY:int;
var worldSizeX:int = 50;
var worldSizeY:int = 50;
var prefab:Transform;
var prefab1:Transform;
var index:int = 0;
var index1:int = 0;
var sunExists:System.Boolean = false;
var bodyScale;
var sun : Transform;//physics variables;
private var _COM : Vector3 = Vector3.zero;
private var _GravConstant : float = 6.673 * Mathf.Pow(10.0, -11.0); // Units: m3 kg-1 s-2
private var _SimTime : float = 0.0; // D (Days)
private var _Mass : float = 0.0;private var _DistanceConversion : float = 6.68458712 * Mathf.Pow(10.0, -12.0); // m → AU (Astronomical Units)
private var _MassConversion : float = 1.67403241 * Mathf.Pow(10.0, -25.0); // kg → E (Earth masses)
private var _TimeConversion : float = 1.15740741 * Mathf.Pow(10.0, -5.0); // s → D (Days)
var nextPlanet : Transform;function Update(){
-
if(index <= 9){*
-
//generates a random position on the horizontal plane that is inside the world size values.*
-
var position: Vector3 = Vector3(Random.Range(-worldSizeY, worldSizeX), 0, Random.Range(-worldSizeY, worldSizeY));*
-
objectList.push(position);*
-
index1++;*
-
}*
-
//if there is a sun, dont make another. If not, create a sun.*
-
if(sunExists == false){*
-
sun = GameObject.Instantiate(prefab1, new Vector3(0,0,0), Quaternion.identity);*
-
objects[0] = GameObject.sun;*
-
masses[0] = 100;*
-
sunExists = true;*
-
}*
-
//makes nine bodies to orbit the sun.*
-
if(index <= 9){*
-
//creates the scale of the next body*
-
bodyScale = Random.Range(2,12);*
-
//sets the body’s mass*
_ masses[index+1] = 5.9742* Mathf.Pow(10, 24);_ -
//sets the scale of the next body*
-
prefab.localScale = Vector3(bodyScale, bodyScale, bodyScale);*
-
//instantiates a new body and adds to the GameObject array*
-
nextPlanet = GameObject.Instantiate(prefab,objectList[index],Quaternion.identity);*
-
objects[index] = GameObject.nextPlanet;*
-
//adds one to the index*
-
index++;*
-
}*
-
// Convert the gravitational constant’s units*
_GravConstant *= Mathf.Pow(_DistanceConversion, 3.0); -
_GravConstant /= _MassConversion;*
-
_GravConstant /= Mathf.Pow(_TimeConversion, 2.0);*
-
applyForces();*
}
function applyForces(){ -
// Initialize the kinetic and potential energies*
-
var tKineticEnergy : float = 0.0;*
-
var tPotentialEnergy : float = 0.0;*
-
// Initialize the NxN matrix to store the gravitational forces between each body-pair*
-
var tForcesMatrix : Array = new Array[objects.length];*
-
// Initialize a matrix to store the total forces to apply on each body*
-
var tTotalForcesArray : Vector3 = new Vector3[objects.length];*
-
// Walk each body in the system*
-
for (var i : int = 0; i < objects.length-1 ; i++) {*
-
// Initialize this body's gravitational forces array and total force value*
-
var tForcesArray : Vector3[] = new Vector3[objects.length];*
-
var tTotalForce : Vector3 = Vector3.zero;*
-
//var tSeperationVector : Vector3 = Vector3.zero;*
-
var tForceValue : float;*
-
// Walk each body-pair in the system*
-
for (var j : int = 0; j < objects.length-1; j++) {*
-
var tSeparationVector : Vector3 = new Vector3(0,0,0);*
-
// Check the body-pair indices*
-
if (i != j) {*
-
// Calculate the gravitational force between these two bodies*
_ tSeparationVector = objects[j].transform.position - objects*.transform.position;_
tForceValue = (_GravConstant * masses _ masses[j]) / tSeparationVector.sqrMagnitude;
tForcesArray[j] = tSeparationVector.normalized * tForceValue;_
* // Update the potential energy if necessary*
_ if (i < j) { tPotentialEnergy -= (tForceValue * tSeparationVector.magnitude); }_
* } else {*
* // Use a zero vector, no self gravitation*
* tForcesArray[j] = Vector3.zero;*
* }*
* // Increment the total force to apply*
* tTotalForce += tForcesArray[j];*
* }*
* // Add this body’s forces array to the matrix and total force to the array*
_ tForcesMatrix = tForcesArray;
tTotalForcesArray = tTotalForce;
* }*_
* // Finalize the center of mass vector*
_COM = (1.0 / _Mass) * _COM;
* // Apply the total force to each body in the system*
* for (var k : int = 0; k < objects.length-1; k++)*
* {*
* objects[k].rigidbody.AddForce(tTotalForcesArray[k]);*
* }*
}
Sorry for the wall of code, but I think that in order to find the problem, it is necessary to see everything that I am doing.
Thanks so much for your help.