- Home /
2d Endless Runner: Deleting objects behind player and platform limit
I am making a 2d endless running game. I currently have a platform generating script with the following Update function:
function Update () {
if(platformCount!=platformCount-recycle.stdest){
platformCount-=recycle.stdest;
}
if (locked==false &&platformCount<=platformCap){
Generate();
and a script deleting previous platforms:
#pragma strict
var objToCheck: GameObject;
var distance:float;
var destroyed:int;
static var stdest:int;
function Update () {
objToCheck=GameObject.FindWithTag ("platform");
distance = Vector3.Distance(objToCheck.transform.position, transform.position);
if (distance>100){
Destroy(objToCheck);
destroyed++;
stdest=destroyed;
}
}
}
It is designed to only have 30 platforms at a time, to reduce any lag. It works fine until the first platform gets deleted, then the cap becomes useless and infinite platforms are generated. Does anyone have an idea of what is going on?
EDIT: I just realized what i did(a number minus another number will never equal itself unless the number is 0) but im not sure how i should go about properly using my scripts to keep the number of platforms at 30.
Answer by KellyThomas · Feb 02, 2014 at 03:17 AM
GameObject.FindWithTag()
will return a single object, it will return the first one it finds using it's internal algorithm, as far as we are concerned it could be any of them.
You will need to iterate though the collection returned by GameObject.FindGameObjectsWithTag()
if you want to apply you logic to all of the platforms.
You may find that your record keeping could be simplified by using GameObject.FindGameObjectsWithTag().Length
okay thanks, but im having trouble getting this to work. i created the array, but how should i go about deter$$anonymous$$ing when to destroy the platform?
Never$$anonymous$$d, i figured it out. i made it destroy any platforms more than 30 units behind me. Thanks for the help:)
Answer by Rhylvin2015 · Jul 31, 2015 at 08:07 AM
Hello I know this thread is an old one.. but ive got the same problem. Can you give me the codes that you created that you figured out.. im a clutz at creating array.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to use a local var in the script 2 Answers
Make Animator bool false when object is destroyed 0 Answers
getchild(0), but first child is gonna be destroyed! 1 Answer
Finding any objects below or above object at ANY POINT? 1 Answer