- Home /
Why does my program crash when allocate and try to store variable in Vector3 array?
Hi, I am trying to compare two Vector3 which are coordinates to an instantiated object within a margin so that it doesn't instantiate the second object in the same position. But before that I need to store the correct values in an array to compare the current number with in later calculations. But I keep getting either NullReferenceException: Object reference not set to an instance of an object or worse Unity crashes without giving me a bug report. What to do?
my main class:
public Circle_gen gen;
void Awake () {
gen = gameObject.GetComponent<Circle_gen> ();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
gameObject.GetComponent<Circle_gen>();
if (Input.GetMouseButtonDown (0)) {
gen.reset ();
gen.clear ();
gen.initializePositions ();
gen.StartCoroutine("generateABC");
}
}
my Circle_gen class:
public GameObject prefabA;
public GameObject prefabB;
public GameObject prefabC;
private GameObject aClone;
private GameObject bClone;
private GameObject cClone;
private GameObject A;
private GameObject B;
private GameObject C;
private int numA;
private int numB;
private int numC;
private Vector3 posA, posB, posC;
private bool check = false;
private float currentX, currentY;
private int size = 0;
public Vector3[] positions; //maybe public for text
private Vector3 position;
private float xMax, xMin, yMax, yMin;
private float margin = 3.0f;
public void initializePositions () {
positions = new Vector3[20];
}
public IEnumerator generateABC(){
Vector3 center = transform.position;
numA = 8;
for (int a = 0; a < numA; a++){
if (numA != 0) {
yield return new WaitForSeconds (1);
}
while (check == false) {
posA = RandomCircle (center, 3.0f);
checkPosition (posA);
}
check = false;
Quaternion rotA = Quaternion.LookRotation(posA-center);
A = (GameObject)Instantiate(prefabA, posA, rotA);
A.name = "aCube"+ a;
}
numB = 8;
for (int b = 0; b < numB; b++){
if (numB != 0) {
yield return new WaitForSeconds (1);
}
while (check == false) {
posB = RandomCircle (center, 2.0f);
checkPosition (posB);
}
check = false;
Quaternion rotB = Quaternion.LookRotation(posB-center);
B = (GameObject)Instantiate(prefabB, posB, rotB);
B.name = "bCube"+ b;
}
numC = 4;
for (int c = 0; c < numC; c++){
if (numC != 0) {
yield return new WaitForSeconds (1);
}
while (check == false) {
posC = RandomCircle (center, 1.0f);
checkPosition (posB);
}
check = false;
Quaternion rotC = Quaternion.LookRotation(posC-center);
C = (GameObject)Instantiate(prefabC, posC, rotC);
C.name = "cCube"+ c;
}
}
private Vector3 RandomCircle ( Vector3 center , float radius ){
Vector3 pos;
float ang = Random.value * 360;
pos.x = center.x + radius * Mathf.Sin (ang * Mathf.Deg2Rad);
pos.y = center.y;
pos.z = center.z + radius * Mathf.Cos (ang * Mathf.Deg2Rad);
return pos;
}
private void checkPosition (Vector3 currentPos) {
if (positions != null) {
currentX = currentPos.x;
currentY = currentPos.y;
for (int i = 0; i < positions.Length; i++) {
position = positions [i];
xMax = position.x + margin;
xMin = position.x - margin;
yMax = position.y + margin;
yMin = position.y - margin;
if (currentX > xMax && currentX < xMin && currentY > yMax && currentY < yMin) {
print ("ok");
size++;
positions [size] = currentPos;
check = true;
} else {
print ("not ok");
}
}
} else {
positions [0] = currentPos;
check = true;
}
return;
}
public void clear (){
if (positions != null){
positions = null;
}
}
public void reset(){
for (int a = 0; a < numA; a++) {
aClone = GameObject.Find ("aCube" + a);
if (aClone != null) {
Destroy (aClone);
}
}
for (int b = 0; b < numB; b++) {
bClone = GameObject.Find ("bCube" + b);
if (bClone != null) {
Destroy (bClone);
}
}
for (int c = 0; c < numC; c++) {
cClone = GameObject.Find ("cCube" + c);
if (cClone != null) {
Destroy (cClone);
}
}
}
Thank you for any help you could give me.
had a quick glimpse, but the most obvious is that you start a new Coroutine each update in circle-gen while the Coroutine itself yields a lot. since it's the same instance in a very short time there is a while lot going on there, probably the rain for crashing unity
Thank you for your answer. The new Coroutine is started only when key is pressed so not on each update (or so i thought). Also in a previous version of the code I had split up the Coroutine in 3: generate A, generate B and generate C – where Coroutine A would start B when it was completed and be would do the same to B. Would that solve my problem? Also the code worked fine until I tried storing and checking values in a Vector3 array.
$$anonymous$$aybe the check field is never getting set to true, it might not hit the criteria?
Answer by NoDumbQuestion · May 30, 2018 at 07:27 AM
Line:70
while (check == false) {
posC = RandomCircle (center, 1.0f);
checkPosition (posB);
}
This will loop 4ever since check always == false. Nothing make check == true
checkPosition method makes check true in some cases. But yes there may be cases where check will never be true which will be the main issue.
If the number being checked is the same or similar to a number in the array compared to x$$anonymous$$ax, x$$anonymous$$in, y$$anonymous$$ax, y$$anonymous$$in then check stays false which should call the randomCircle function to calculate a new vector3 to be checked again till it is checked true. At least that’s how I would like it to work. I do need to verify as you said if the boolean is switching from false to true or if it is an infinit loop.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initializing an array of Vector3 in c sharp # 3 Answers
Making 'table' (2D built-in array) of GameObjects giving "NullReferenceException:" error 1 Answer
,How to store multiple Vector3s inside of a single variable every frame? 1 Answer
Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer