Search Unity

How do you duplicate an object and delete the old one?

Discussion in '2D' started by cyclSunity, Jun 13, 2020.

  1. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Is there a reason you aren't just using a prefab of the object? Also, duplicating and destroying seems rather pointless to me. Why not just keep the original around?
     
  3. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    Because, they need to duplicate so it's infinite. When the old one moves off the screen, I want to
    Destroy();
    it so it doesn't lag.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Creating a copy of something then destroying the original is extremely wasteful. Typically what happens is that when stuff moves off screen, it's repositioned off screen ahead. This is a simple transform operation as opposed to a relatively expensive clone/destroy. You can even use a more sophisticated object pooler to store stuff away when it goes off sceeen back to the pool and ask for stuff out of the pool when you need it; this reduces the cloning/creating costs.
     
    rgameiro likes this.
  5. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    So what do I do to clone them? Don't I use
    Instantiate();
    or something? If so, I knew that I should use
    Instantiate();
    but I don't know what arguments to put in.
     
  6. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    You can change the sprite to a different pipe and teleport it back to the right side
     
  7. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
  8. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    How?
     
  9. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
  10. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    What arguments can I use for
    Instantiate();
    and
    Destroy();
    ?
     
  11. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
  12. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Code (CSharp):
    1. SpriteRenderer.Sprite = (your new sprite)
    2.  
    3. transform.position = new vector3 ( 1000, 0,0);//for example
     
  13. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    transform.position
    would move it, not teleport it!
     
  14. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    ah but isnt teleport movement? just really fast movement, instantaneous wouldnt you say? Sounds like a teleport to me!
     
  15. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    What is "your new sprite" supposed to mean?
     
  16. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    well, if you want to change the pipe sprite thats how you would do it.

    You can ignore it if you don't need to change the sprites
     
  17. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    Ok brigas! So this will work if someone tells me how to use a coroutine to
    WaitForSeconds();
    .
     
    Last edited: Jun 18, 2020
  18. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         StartCoroutine(WaitAndLog(2f)); // starts coroutine
    4.     }
    5.  
    6.     // suspend execution for waitTime seconds
    7.     IEnumerator WaitAndLog(float waitTime)
    8.     {
    9.         yield return new WaitForSeconds(waitTime); // waits for x seconds
    10.         Debug.Log("WaitAndLog " + Time.time);
    11.     }
     
  19. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    I'm guessing 2f is how long to wait?
     
  20. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    yes, in the example 2 seconds
     
  21. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    Ok, cool.
     
  22. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    No, it's not. I just want to clone them to make the game infinite. But since it will lag if I don't delete the old ones, I want to
    Destroy();
    the old ones
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I'm afraid it is but you are free to do it however you like. Recycling objects also known as pooling or simply changing the Transform of objects to reposition them as they go off screen to appear ahead of a player is a very common method.
     
  24. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123




    IT IS NOT!!!!!!!!!
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    You are entitled to your opinion. The matter is closed from my point of view. Good luck with your project.
     
  26. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    Thanks but I haven't really been working on it also I'm hosting a game jam so anyone who sees this can join here: https://itch.io/jam/myfirstgamejam
     
  27. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    wtf bro
     
    Armynator likes this.
  28. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
  29. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    cringe xd
     
    Armynator likes this.
  30. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    Uhh...
     
  31. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    I'm so confused bruh