Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Silver96 · May 29, 2018 at 09:16 PM · arrayvector3nullreferenceexceptionstoreinitialize

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · May 30, 2018 at 05:17 AM 0
Share

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

avatar image Silver96 hexagonius · May 30, 2018 at 06:15 AM 0
Share

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.

avatar image ShadyProductions Silver96 · May 30, 2018 at 06:58 AM 0
Share

$$anonymous$$aybe the check field is never getting set to true, it might not hit the criteria?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · May 30, 2018 at 10:18 AM 0
Share

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.

avatar image Silver96 · May 30, 2018 at 10:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initializing an array of Vector3 in c sharp # 3 Answers

get the average position of objects in array? 1 Answer

,How to store multiple Vector3s inside of a single variable every frame? 1 Answer

Error putting an object into an Array 0 Answers

Accessing Instance Bool from other script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges