- Home /
How can I loop objects with Polygoncollider2D attached to them?
I have a background looper script which is looping all the objects in the scene with the Boxcollider2D attached to them. I have now Polygoncollider2D objects in the scene but they don't loop due to the fact they don't have the box collider attached to them. I use the following script to make the box collider loop in the background looper;
using UnityEngine;
using System.Collections;
public class BGLooper : MonoBehaviour {
int numBGPanels = 4;
void OnTriggerEnter2D(Collider2D collider) {
Debug.Log ("Triggered: " + collider.name);
float widthofBGObject = ((BoxCollider2D)collider).size.y;
Vector3 pos = collider.transform.position;
pos.y += widthofBGObject * numBGPanels;
collider.transform.position = pos;
}
}
The problem is located in the part, describing its size (.size.y) as can be seen below:
I know Polygon colliders work with points instead of size but I wasn't able to implement it in this script. If u have any suggestions or recommendations please let me know :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613182902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating 2D collider from 3D mesh 0 Answers
Colliders not working with Character VS House 0 Answers
PolygonCollider2D shape problem on iOS 0 Answers
Collision in between 2 objects moving away from each other? 1 Answer
How to keep the orientation of an object unchanged when applying forces on its sphere collider? 1 Answer