Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

Adressing a Child Object after it´s unparented

$
0
0
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().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().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?

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>