Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Copying my object to memory ONLY, no instantiate.

Discussion in 'Scripting' started by shivansps, Jan 25, 2015.

  1. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    So, i need to return my fighters to the hangar.

    Code (JavaScript):
    1. function ReturnHangar()
    2. {
    3.     if(HomeHangar!=null)
    4.     {
    5.         HomeHangar.CurrentShips-=1;
    6.         if(HomeHangar.Player)
    7.         {
    8.             HomeHangar.Landed.Add(this);
    9.             HomeHangar.Deployed.Remove(this);
    10.         }
    11.     }  
    12.     Destroy (gameObject);
    13. }
    So, this is the tricky part, i cant do that because in the moment im destroying its gona null at the "Landed" list, i need to save the variables so i cant use a reference to the original object. I cant use Instantiate because a copy of the object is created at screen, and i dont want that, i just want a copy of the object in memory.
     
  2. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    I think it cant be done, i ended workaround it by de-activating the object and move it outside other ships "radar range" instead, them i activate it and move it back, i think thats may be even better that what i was trying to do.
     
    Last edited: Jan 26, 2015
  3. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Ya that's what I would do, since your going to re use it anyways. Also when you deactivate it, its update loop stops anyways and it no longer tests for collisions.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Instead, have it so that the other ship doesn't include disabled ships as "radar-seeable", rather than moving disabled ships around. Seems less messy.
     
  5. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Yeah, that whould be the best for a 3D game, but since im working on a 2D game i think its better to just parent it with the hangar at return and send it "out of range" in the Z, that is like -2, because the "radar" is 2D too, instead of adding a extra "if" to something that every ship do all the time constantly.