Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Recreate in 1 single frame

Discussion in 'Scripting' started by equal, May 25, 2016.

  1. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    Hi,

    i have the current problem that Destroy happens at the end of the frame and DestroyImmediate is strongly discouraged to use. so how to go about recreation in 1 frame?

    There are Ships , those Ships have shipparts and those have blocks.

    The shipparts have a Data Array behind them with the important info like hp of blocks etc.

    The Funktion Shippart.UpdateShippart() is supposed to kill everything it has, read its data and create a block for every block in its Data Array.

    What i tried / what can not be used:
    A bool in the Update funktion (because this would lead to every shippart having an update funktion)
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    IM not sure exactly what you need but everything in update in every script will be executed each frame so you should be no problem to destroy and instantiate in one frame. Try a for loop to scan the array and destroy/create each block instead of a bool in the update.

    here are loop examples

    Code (CSharp):
    1. foreach(Data d int dataArray)
    2.     {
    3.         Destroy(do);
    4.         //or whatever
    5.     }
    6.     for(int i = 0; i < dataArray.Length; i++)
    7.     {
    8.         //do whatever you need to do with dataArrray[i]
    9.     }
    10.  
     
  3. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    Sorry i was not more specific, i mean more like this:

    Code (CSharp):
    1.  
    2. public void recreator(){
    3. int c = 0;
    4. foreach(Data d int dataArray)
    5.     {
    6.         c++;
    7.         Destroy(do);
    8.         //or whatever
    9.     }
    10.     for(int i = 0; i < c; i++)
    11.     {
    12.         instantiate(someObj);
    13.     }
    14. }
    In a single frame run.
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Garbage collection is not going to like this at all (avoid destroying objects as much as you can, reuse them if possible), but Update() is the place to do it. Have 1 object controller gameobject that is the only one having the script. Then something like:
    Code (CSharp):
    1. void Update() {
    2.   ... Destroy objects here...
    3.  
    4.   ... Instantiate objects here...
    5. }
    This should make them appear for 1 frame duration.

    Also should mention that this will most likely make your game very slow, compared to how it could perform.