- Home /
Collider not resizing with Sprite Shape by Script
I built a Random Generated Mini Game with SpriteShape Prefabs. The Random Generator moves and changes the Spline of the SpriteShape. But the Edge Collider 2D refreses its size only when I click on the Instantiated Prefab on the Hirarchy.
What can I do to Refresh the Collider
This is the Scene when the World just generated. All Colliders are on the left side.
This is the Scene after I clicked on the first 2 generated layers. The 2 Colliders Refreshed its size to the SpriteShape
This is our code:
GameObject currentLayer;
Spline spline;
int lastXPos = Random.Range(1, maxXPos-2);
currentLayer = Instantiate(layer);
currentLayer.transform.parent = gameObject.transform;
currentLayer.transform.localPosition = new Vector3(0,-(2+i*2));
spline = currentLayer.GetComponent<SpriteShapeController>().spline;
spline.SetPosition(0, new Vector3(0, 0));
spline.SetPosition(1, new Vector3(lastXPos, 0));
Answer by ParadoxedOne · Apr 08, 2019 at 06:28 PM
Where are your voids? This code isn't complete enough for me to figure out what's broken...
Do you found an answer? Or have an Ideo of how we can do this better without tilemaps?
Answer by mrbrable · Apr 08, 2019 at 06:33 PM
This is the complete Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class MapGeneratorDropper : MonoBehaviour
{
[Space(5)]
public GameObject layer;
[Space(10)]
public GameObject leftWall;
public GameObject rightWall;
int layerCount;
int maxXPos = 8;
void Awake(){
layerCount = 5;//Random.Range(10, 20);
maxXPos = (int) rightWall.GetComponent<SpriteShapeController>().spline.GetPosition(1).x;
for(int i=0; i<layerCount; i++){
SpawnLayer(i);
}
}
void SpawnLayer(int i){
GameObject currentLayer;
Spline spline;
int lastXPos = Random.Range(1, maxXPos-2);
currentLayer = Instantiate(layer);
currentLayer.transform.parent = gameObject.transform;
currentLayer.transform.localPosition = new Vector3(0,-(2+i*2));
spline = currentLayer.GetComponent<SpriteShapeController>().spline;
spline.SetPosition(0, new Vector3(0, 0));
spline.SetPosition(1, new Vector3(lastXPos, 0));
}
}