- Home /
2D Array Rigidbody details storing error - C#
I am building this project, scattering planets across space randomly and I want Gravitation to be implied accurately. What I am doing is this: 1 Instantiating a random number of planets and scattering them randomly. 2 The actual planets in game should be stored in the first column of my Rigidbody Array (Dw code coming soon) 3 The rest of the columns should just be virtual references to these planets, think of it like having the exact same thing, but just not the planet in game. Example : someArray[somenumber,somenumberaswell] In the first column, that is somenumber=0, the actually visible Rigidbodies in game are stored. In the rest of the columns, their properties are stored. Like the second, third fourth and so on...: someArray[1,somenumberaswell]= some rigidbody's mass; Now here is the code, it is rather long so I put in some helpful comments:
using UnityEngine;
using System.Collections;
public class Gravity : MonoBehaviour {
public GameObject PlanetCore; //yes it HAS a rigidbody :D
public static int pnum;
public static Rigidbody[,] myPlanets;
public GameObject[,] obj;
public Vector3[,] centerofmass;
public int referencer,looper;
public Transform[,] objT;
public float[,] distanceBetweenPlanets;
void Start ()
{
pnum = Random.Range(10,15); //a random number of planets
myPlanets = new Rigidbody[pnum,pnum] ; //they are both set pnum for the reason I proposed
obj = new GameObject[pnum,pnum];
objT = new Transform[pnum,pnum];
centerofmass=new Vector3[pnum,pnum];
radtodeg=new float[pnum];
Vector3 planetPosition;
//Instantiates planets and scatters them randomly :
for(looper=0;looper<myPlanets.GetLength(0);looper++){
if(1+looper==1){//so this only runs ONCE
for(int looper2=0;looper2<myPlanets.GetLength(0);looper2++){
planetPosition = new Vector3(Random.Range(-30f,30f),Random.Range(-30f,-30f),Random.Range(-30f,30f));
obj[0,looper2] = (GameObject)Instantiate(PlanetCore,planetPosition,Quaternion.Euler((Random.value+1)*360f,(Random.value+1)*360f,(Random.value+1)*360f));
}
}
//Destroys the planet which was being used to duplicate:
Destroy(PlanetCore);
looper=1;//looper set to 1 because the 0 column is already set
//This is where the problems start: Specifically in the first line under the loop header. so probably in every other line in the loop as well.
for(referencer=0;referencer<myPlanets.GetLength(0);referencer++){
myPlanets[0,referencer]=obj[looper,referencer].rigidbody;
myPlanets[0,referencer].mass=(Random.value+1)*4000f;
objT[0,referencer]=obj[looper,referencer].transform;
centerofmass[0,referencer]=obj[looper,referencer].rigidbody.worldCenterOfMass;
if(referencer+1==myPlanets.GetLength(0)){
looper++;
referencer=0;
}
}
//Just names the planets: Planet0, Planet1, etc etc...
for(int i=0;i<myPlanets.GetLength(0);i++){
obj[0,i].name = "Planet"+i;
}
}
}
Now the error I keep getting right under that for loop header is this: Object reference not set to an instance of an object. And I keep staring at it and it stares at me and I am like WHY?!
Thanks a lot in advance, I know the code is rather long and might not get an answer, but thanks ! :D
Answer by RakshithAnand · Jul 01, 2014 at 09:59 AM
Please use Classes to store property values and also generic Dictionaries are very useful if you know the type before hand Just put this code in a script called "Planets".
It should do the same job you are trying to do but with a very much better way. Refer to comments for what i am doing. If any help needed ask me
Hope this helps.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Planets : MonoBehaviour {
// Use a prefab
public GameObject planetPrefab;
int numberOfPlanets;
Vector3 randomPosition;
Vector3 randomRotation;
Dictionary<GameObject, PlanetProperties> myPlanets;
// Use this for initialization
void Start ()
{
numberOfPlanets = Random.Range (10, 15);
myPlanets = new Dictionary<GameObject, PlanetProperties> ();
// Keep your start method readable
createRandomPlanets ();
// Now you have a dictionary of planets with their properties.
// You can loop through the dictionary and set different properties if you want
int i = 0;
foreach(KeyValuePair<GameObject, PlanetProperties> myPlanet in myPlanets)
{
i++;
myPlanet.Key.name = "SomeRandomPlanet"+i;
myPlanet.Value.mass = 500000f;
//etc ..
}
}
void createRandomPlanets()
{
for(int i =0; i<numberOfPlanets; i++)
{
randomPosition = new Vector3(Random.Range(-30f,30f),Random.Range(-30f,-30f),Random.Range(-30f,30f));
randomRotation = new Vector3((Random.value+1)*360f,(Random.value+1)*360f,(Random.value+1)*360f);
GameObject newPlanet = Instantiate(planetPrefab, randomPosition, Quaternion.Euler(randomRotation) ) as GameObject;
PlanetProperties newPlanetPorperties = new PlanetProperties();
newPlanetPorperties.mass = (Random.value+1)*4000f;
newPlanetPorperties.transform = newPlanet.transform;
newPlanetPorperties.centerOfMass = newPlanet.rigidbody.worldCenterOfMass;
newPlanetPorperties.position = newPlanet.transform.position;
newPlanetPorperties.planetRef = newPlanet;
// Here i kept a reference to the newly created planet since the dictionaries key is used to just acces the value which is planet properties.
// so keeeping the reference to planet you can modify its name etc..
myPlanets.Add(newPlanet, newPlanetPorperties);// add this new planet with detils to the dictionary
}
}
}
public class PlanetProperties
{
public float mass;
public Transform transform;
public Vector3 centerOfMass;
public Vector3 position;
public GameObject planetRef;
// You can add more properties here like name, color, temp etc
}
I will most definitely give it a try! Thanks I know it was a rather boring question, and I really appreciate your help!
Ok I tried it and it works PERFECT! Thanks man if I could I'd rate you up but I can't yet. Thanks A LOT! :D
You are welcome :) .. try to rate it up once you can ;)
I just noticed that the mass of each planet stays at the default of 1 when instantiated, but it should be different for each planet... - Oh I found the problem, see when you called PlanetProperties you were setting the mass value for only that variable and not for the instantiated object... I wrote it like this ins$$anonymous$$d: GameObject newPlanet = Instantiate(planetPrefab, randomPosition, Quaternion.Euler(randomRotation) ) as GameObject; newPlanet.rigidbody.mass = (Random.value+1)*4000f; PlanetProperties newPlanetPorperties = new PlanetProperties(); newPlanetPorperties.mass = newPlanet.rigidbody.mass;
I did that because i just showed you a way of storing the properties using classes. Yes if you want to set the mass there itself you can do it like that or with the same code you can add a line before adding the planet to dictionary:
newPlanet.mass = newPlanetProperties.mass; // Will give you a proper idea from where the mass value is co$$anonymous$$g
But anyhow both ways are fine!
Your answer
![](https://koobas.hobune.stream/wayback/20220613152856im_/https://answers.unity.com/themes/thub/images/avi.jpg)