- Home /
Can't scale parent object separately from its children sprites
I have a bunch of square tile sprites which are going to be put together as children of an empty transform Board. Once they are there, Board is to be re-scaled to fit the screen. For some reason, though, when I re-scale Board, each individual sprite is also re-scaled by the exact amount that counteracts Board's resizing, such that in total the re-scaling is none.
The following script is a component of Board:
using UnityEngine;
using System.Collections;
public class boardResizer : MonoBehaviour
{
public GameObject sampleSprite;
private SpriteRenderer sr;
private int rows;
private int columns;
void Start ()
{
sr = sampleSprite.GetComponent<SpriteRenderer>();
float worldScreenHeight = Camera.main.orthographicSize * 2;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
rows = PlayerPrefs.GetInt("Rows", 10);
columns = PlayerPrefs.GetInt("Columns", 10);
if(rows >= columns)
{
transform.localScale = new Vector3(
worldScreenHeight / (sr.sprite.bounds.size.y * rows),
worldScreenHeight / (sr.sprite.bounds.size.y * rows), 1);
}
else
{
transform.localScale = new Vector3(
worldScreenWidth / (sr.sprite.bounds.size.x * columns),
worldScreenWidth / (sr.sprite.bounds.size.x * columns), 1);
}
}
}
Does anybody know the culprit? I already checked to make sure that nothing else was re-scaling; when I commented out this code, both the board and the sprites remained at their original scale (1,1,1).
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Child Parent problem 1 Answer
Simulate 'Child' Effect by Script 2 Answers
Problem with finding compnent of immidate children 1 Answer
OnCollisionEnter: Collision takes also the parent tag 1 Answer