So basically I have a gameObject “PlatformA” with a SpriteRenderer attached to it. The Sprite that is used is 9-sliced, so that when you scale it, the borders remain the same size (All of that works fine).
Now I have another gameObject “PlatformAChild” that is a child of the gameObject “PlatformA”. This gameObject possesses a BoxCollider2D, which I want to scale according to the changes of the SpriteRenderer in the parent gameObject.
How can I do that? ticking the “Auto Tiling” box doesn’t do anything because there is no SpriteRenderer attached to the child, only to the parent.
I fixed the problem myself by creating a workaround:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode] //makes the script operate in Editormode
public class ScuffedScript : MonoBehaviour
{
private SpriteRenderer parentSpriteRenderer;
private BoxCollider2D scuffedCollider;
private void Awake()
{
parentSpriteRenderer = transform.parent.GetComponent<SpriteRenderer>();
scuffedCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
scuffedCollider.size = new Vector2(parentSpriteRenderer.size.x, parentSpriteRenderer.size.y+0.1f);
}
}
You simply need to apply the code to the child(the gameObject with the BoxCollider2D that you want to change).