- Home /
 
Adressing a Child Object after it´s unparented
Hey, I have a big ship in my space shooter game which is used as a hub to spawn smaller ship. The small ships are instantiated in the as a child object and are released from the parent ship after 10 seconds, after which a new ship will spawn. During the time when the small ship is parented I want his layer to be set to invulnerable, and change it later to a layer called enemy. When I run the code down below, I found out by console logging that the ship is set to layer enemy for one frame, and then later is set back to layer invulnerable.
         //checking if ship can be launched
         if (shipCooldownTimer <= 0f && en_mov_script.onCamera() && shipInDock == false) 
         {
             //launches random ship
             int shipnumber = Random.Range(0, shipPrefab.Length); // determine which ship will be launched
             Quaternion rotation = transform.rotation * Quaternion.Euler(0,0,180f); //changes z rotation by 180 degrees
             childShip = Instantiate(shipPrefab[shipnumber], transform.position - transform.up * 1.5f, rotation, transform);
 
             //disables movement of ship and set layer to invuln
             childShip.GetComponent<EnemyMovement>().enabled = false; 
             childShip.layer = 10;
 
             //set ship cooldown and shipInDock bool
             shipCooldownTimer = shipLaunchDelay;
             shipInDock = true;
         }
 
         if (shipCooldownTimer <= 1f && shipInDock == true)
         {
             //set layer to enemy
             childShip.layer = 9;
 
             //enables movement of ship
             childShip.GetComponent<EnemyMovement>().enabled = true;
 
             //unchilds ship from bigship 
             childShip.transform.parent = null;
 
             //resets shipInDock bool
             shipInDock = false;
         }
 
               My plan was to give the instantiated object a name (childShip) so I can adress it to change its layer and when a new ship is spawned just overwrite childShip so it refers to the new object. Unfortunately something doesn´t work. Any ideas?
Your answer