Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mcabezas26 · Oct 15, 2015 at 04:53 PM · prefabarraybugdestroy

Generate instances of prefab, then save to GameObject array... How?

I'm trying to make a randomly-generated bridge that explodes when a timer counts down and reaches zero. The bridge is made up of instances of a platform prefab, and is generated with the following code:

 public void RoundGen() //Function for generating the level
     {
         // Set the detonation time to a random integer between 1 and the round number
         Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
         float Target = Mathf.Round(Random.Range(0.5f, 5 * Mathf.Sqrt(GameController.round))*10) / 10; //Set the "correct answer" (Target speed) to a number rounded to the 10ths place
 
         if (GameController.round < 21) //If the level is below 20
         {
             Target = Mathf.Round(Target); //Round the target to the nearest whole number
         }
         while (Target * Time != Mathf.Round(Target * Time))  //While the product of target and time variables is not whole
         {
             Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
             Target = Mathf.Round(Random.Range(1 / GameController.round, 5 * Mathf.Sqrt(GameController.round)) * 10) / 10;
         } // Reassign the two variables until the product is whole
         detonationTimer.text = Time.ToString("f"); //Update the detonation timer
         Distance = Mathf.RoundToInt(Target * Time); // Set the distance based on the detonation time and target speed
         BlockBehavior.Blocks = GameObject.FindGameObjectsWithTag("Platform");
         for (int i = 0; i < Distance; i++)
         { // Instantiate the amount of floor blocks needed to build the bridge (Distance) meters long
             Instantiate(FloorBlocks, (offset + (Vector3.right * i)), Quaternion.identity);
         }
 
 
     }

Countdown script and coroutine:

 void BombCountdown()
     {
         StartCoroutine(BombCountdownCoroutine());
     }
 
     IEnumerator BombCountdownCoroutine()
     {
         float BombTimer = RoundGenerator.Time;
         while (BombTimer > 0 && !PlayerController.fallOver)
         {
             Display.text = (Mathf.Round(BombTimer * 100) / 100).ToString("f");
             BombTimer -= Time.deltaTime;
             yield return new WaitForEndOfFrame();
         }
         ExplodeTrigger = true;
         Debug.Log("Countdown finished.");
     }

Explosion script and coroutine:

 void Update()
     {
         if (Countdown.ExplodeTrigger) // When the countdown is finished
         {
             Piece1.GetComponent<Rigidbody>().isKinematic = false; // Remove all constraints
             Piece2.GetComponent<Rigidbody>().isKinematic = false;
             Piece1.GetComponent<Rigidbody>().useGravity = true;
             Piece2.GetComponent<Rigidbody>().useGravity = true;
             Piece1.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange); // Throw the pieces in random directions
             Piece2.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange);
             StartCoroutine(Explosion()); // Start the explosion coroutine
             Countdown.ExplodeTrigger = false;
         }
     }
 
     IEnumerator Explosion()
     { 
         float TempTimer = 1f;
         while (TempTimer > 0)
         {
             TempTimer -= Time.deltaTime;
             yield return new WaitForEndOfFrame();
         }
         foreach (GameObject blk in Blocks)
         {
             Destroy(blk);
         }
         Debug.Log("Blocks destroyed.");
         yield return new WaitForEndOfFrame();
     }

Piece1 and Piece2 are children objects of the prefab. When I try this code, 1) only the first block is blown up. 2) the clone isn't destroyed. 3) The code only works once i.e. once the next level generates, the code no longer works.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dkjunior · Oct 15, 2015 at 10:55 PM

In your RoundGen() method, try the following:

 BlockBehavior.Blocks = new GameObject[Distance];
 
 for (int i = 0; i < Distance; i++)
 { // Instantiate the amount of floor blocks needed to build the bridge (Distance) meters long
 BlockBehavior.Blocks[i] = Instantiate(FloorBlocks, (offset + (Vector3.right * i)), Quaternion.identity) as GameObject;
 }

I assume that BlockBehavior.Blocks is defined as GameObject[], and that FloorBlocks is your platform prefab.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Parenting an instantiated prefab. 1 Answer

Destroying Instantiated Prefab 1 Answer

Prefabs into array than delete them when user interacts 1 Answer

Destroying Object without removing attached prefab, How can I destroy an object without destroying the original prefab attached? 1 Answer

Destroy instances from an array 3 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