unity3d - Object not destroying after reaching a certain point Unity -
okay i'm making infinite runner , technique i've used keep player static , move , instantiate platform objects. i've created array list of objects platforms. , i'm adding them in , spawning them. translating them along z-axis , want destroy objects go below 0 in z axis , add object in replacement. problem doesn't translate objects unless add script , doesn't destroy or add if add script translation.
my code follows. if have trouble understanding problem, please ask try explain again.
i have 2 scripts.
1) platformmanager: 1 applied on empty gameobject , contains arraylist.
public class platformmanager : monobehaviour { [hideininspector] public list<gameobject> platforms = new list<gameobject>(); // list of platfroms. public gameobject[] prefab; // allow user add many prefabs through inspactor. public static float noofplatforms; // variable hold how many platoforms. [hideininspector] public static float objectposition; // z position of game object. [hideininspector] public static float objectscale; // use initialization void start () { noofplatforms = 6.0f; objectposition = 0.0f; objectscale = 0.0f; platforms.add((gameobject)instantiate(prefab[random.range(0,prefab.length)], new vector3(0,0,0) ,quaternion.identity)); //platforms.add((gameobject)instantiate(prefab[random.range(0, prefab.length)], new vector3(0, 0, 40.40114f), quaternion.identity)); for(int = 0; < noofplatforms; i++){ objectscale = platforms[platforms.count-1].transform.localscale.z; objectposition = platforms[platforms.count-1].transform.localposition.z; platforms.add((gameobject)instantiate(prefab[random.range(0,prefab.length)], new vector3(0,0,(objectposition + objectscale)+277f) ,quaternion.identity)); } } // update called once per frame void update () { } }
2) platformcontroller: 1 supposed instantiate , destroy , translate objects.
public class platformcontroller : monobehaviour { //private bool canbedestroy = true; // flag check whether gameobject shoud destroyed or not. [hideininspector] public platformmanager manageplateform; // reference plateformmanager script. [hideininspector] public float plateformspeed = 10f; [hideininspector] public gameobject allplatforms; [hideininspector] // awake called when script instance being loaded. void awake() { // accessing plateformmanager script plateform. manageplateform = gameobject.findgameobjectwithtag("platform").getcomponent<platformmanager>(); } void start() { } // update called once per frame void update() { // first platform object. gameobject firstplatform = manageplateform.platforms[0]; // last platform object. gameobject lastplatform = manageplateform.platforms[manageplateform.platforms.count - 1]; if (firstplatform.transform.localposition.z < 0f) { destroy(firstplatform.gameobject); // destroy first plateform gameobject. manageplateform.platforms.remove(firstplatform); // remove destroyed object list. // when game object destroyed instantiate 1 gameobject list , add @ last point. manageplateform.platforms.add((gameobject)instantiate(manageplateform.prefab[random.range(0, manageplateform.prefab.length)], new vector3(0, 0, (lastplatform.transform.localposition.z + lastplatform.transform.localscale.z) + 277f), quaternion.identity)); } // move available platforms in list along z-axis foreach (gameobject platform in manageplateform.platforms) { platform.transform.translate(0, 0, -plateformspeed * time.deltatime); } } }
Comments
Post a Comment