My goal is to spawn a bunch of different platform prefabs with the tag “Platform”, and have them spaced out just after spawn(assuming one spawns on top of another prefab) The prefabs have an edge collider to walk on, a 2d box collider to push them away if they hit the sides of the platform, and another box collider surrounding the entire object that’s used as a trigger to set off the movement:
using UnityEngine;
using System.Collections;
public class PlatformCollisionHandler : MonoBehaviour {
BoxCollider2D _thisBox;
void Awake()
{
_thisBox = this.GetComponent<BoxCollider2D>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Platform")
{
movePlatform(other.gameObject);
}
}
void movePlatform(GameObject thePlatform)
{
if (_thisBox.IsTouching(thePlatform.gameObject.GetComponent<BoxCollider2D>()))
{
this.GetComponent<Transform>().position = this.GetComponent<Transform>().position + new Vector3(1f, 0, 0);
movePlatform(thePlatform);
}
else
{
return;
}
}
}
My problem is, when I go to debug, the platforms are still spawning on top of each other and only the if statement is reached