0

Okay so i'm making an infinite runner and the technique I've used is to keep the player static and move and instantiate the platform objects. For that I've created an Array List of objects that are the platforms. And then I'm adding them in and spawning them. I am also translating them along the z-axis and i want to destroy the objects that go below 0 in the z axis and then add another object in replacement. The problem is that it doesn't translate the objects unless I add another script just to do that and it doesn't destroy or add even if I add another script for translation.

My code is as follows. If you have trouble understanding my problem, please ask I would try to explain again.

I have two scripts.

1) PlatformManager: This one is applied on the empty GameObject and contains the ArrayList.

public class PlatformManager : MonoBehaviour
{
    [HideInInspector]
    public List<GameObject> platforms = new List<GameObject>(); // List of Platfroms.
    public GameObject[] prefab; // Allow user to add as many prefabs through the inspactor.

    public static float noOfPlatforms;  // a variable to hold how many platoforms.
    [HideInInspector]
    public static float objectPosition; // Z position of the game object.
    [HideInInspector]
    public static float objectScale;

        // Use this for 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 i = 0; i < 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 is called once per frame
        void Update ()
        {

        }
}

2) PlatformController: This one is supposed to instantiate and destroy and translate the objects.

public class PlatformController : MonoBehaviour {
    //private bool canBeDestroy = true; //  Flag to check whether the gameObject shoud be destroyed or not.
    [HideInInspector]
    public PlatformManager managePlateform; //  Reference to plateformManager script.

    [HideInInspector]
    public float plateformSpeed = 10f;
    [HideInInspector]
    public GameObject allPlatforms;
    [HideInInspector]


    //  Awake is called when the script instance is being loaded.
    void Awake()
    {
        //  Accessing the plateformManager script for the Plateform.
        managePlateform = GameObject.FindGameObjectWithTag("Platform").GetComponent<PlatformManager>();
    }

    void Start()
    {

    }


    //  Update is called once per frame
    void Update()
    {

        //  Get the first platform object.
        GameObject firstPlatform = managePlateform.platforms[0];
        //  Get the last platform object.
        GameObject lastPlatform = managePlateform.platforms[managePlateform.platforms.Count - 1];

        if (firstPlatform.transform.localPosition.z < 0f)
        {
            Destroy(firstPlatform.gameObject); // destroy the first plateform gameObject.

            managePlateform.platforms.Remove(firstPlatform); // also remove the destroyed object from the list.

            // When the game object is destroyed then instantiate one gameobject into list and add at the 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 the available platforms in the list along the z-axis
        foreach (GameObject platform in managePlateform.platforms)
        {
            platform.transform.Translate(0, 0, -plateformSpeed * Time.deltaTime);
        }
    }

}
Rune Vejen Petersen
  • 3,201
  • 2
  • 30
  • 46
momal
  • 576
  • 2
  • 13
  • 30
  • Have you tried to debug the code when you expect a collision? – Jepessen Aug 18 '14 at 08:09
  • I don't expect a collision, sir. – momal Aug 18 '14 at 08:10
  • I noticed you use lastPlatform.transform.localPosition.z does that mean the platforms a child to another object? In that case you need to set the parent by transform.parent = ... – Imapler Aug 18 '14 at 11:33
  • Do you get any Exceptions and what exactly is your problem? Does everything workand you just want to have the scripts seperated and ordered differently? Could you explain a bit more detailed what exactly you need help with? – T. Grumser Dec 04 '17 at 16:57
  • @T.Grumser I asked this question 3 years ago. I quit creating games and I do not even understand what I was asking here. So I guess, this question should be marked closed or should be deleted since I got no answers for it. – momal Dec 05 '17 at 13:02

0 Answers0