Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Fruit Ninja 2

Need Help Unity 2017 Crahsed Instantiate gameObject

Question

I'm sorry for my English.
My Unity 2017.3.0f2 worked fine yesterday, but when I loaded my project today, it froze when I tried to create an instance of the object, identify the code that caused this error and I share it with you, I do not know what the problem was inside the code, it worked fine yesterday , I thought that the cycle produces this error, but I do not see anything wrong inside the code, please need help with my code, what I try is to capture all the Gameobjects in front of the object, and that they are saved in an array, to later use the position of these GameObject to instantiate a bomberman-style bomb, and check for hits.Length greater than zero. Here I leave the code already commented:

 

    void CaptureAllDelante(){
        RaycastHit[] hits;
        hits = Physics.RaycastAll(transform.position, Vector3.forward, Potencia);

        int i = 0;
        print ("Check if hits.length greater zero
// " + hits.Length.ToString());
        while (i < hits.Length) {
 
            if (hits [i].transform.tag == "Paredes" || hits [i].transform.tag == "Player" ) {
                break;

                //"if object tag is Paredes / Player, Stop while"
            } else if(hits [i].transform.tag == "Destructibles"){

                //"if object tag is Destructibles , save object in array ObjetosDelante for use the position later"
                RaycastHit hit = hits[i];
                ObjetosDelante [i] = hits [i].transform.gameObject;
                Debug.Log (hit.collider.gameObject.tag.ToString()); 
                i++;
            }
        }

        for (int a = 0; a < ObjetosDelante.Length; a++) {
            if (ObjetosDelante[a] == null) {
                break;
            }

            //"if ObjetosDelante if != null instantiate Explosion in each position of object in array"
            Instantiate (Explosion, ObjetosDelante[a].transform.position,Quaternion.identity); 
        }


        SearchObjetoDelante = false;

    }

Edited by Fruit Ninja 2

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 1

move  i++; out of "else if", or instead of "else if" use "else" and then "if(hits .transform.tag == "Destructibles"){}  i++ ".

Share this post


Link to post
Share on other sites

  • 0
Please login or register to see this quote.

thank you very much that was the solution, yesterday it seems that I made the mistake of putting the i ++ inside the else and I did not realize. Thank you.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...