- Home /
While the Parent expands, the children get smaller. Problem
Hello. I am using a script that when a mouse clicks goes off a certain object all objects linked to the same parent will expand. The parent and the objects do get bigger in a way I'd like. For some reason however, the objects inside the parent ( Empty game object ) get smaller and smaller as you progress into the game. How can this be fixed? This is the script
using UnityEngine;
using System.Collections;
public class Expand : MonoBehaviour {
public Transform parent;
public float expandAmount = 2f;
void OnMouseDown()
{
parent.localScale *=expandAmount;
for(int i =0;i<parent.childCount;i++)
{
parent.GetChild(i).localScale = parent.GetChild(i).localScale/expandAmount;
}
}
}
If clarification on anything is needed, just ask. Thanks
Answer by OmniKeith · Apr 20, 2015 at 03:11 AM
They get smaller because you are dividing( / ) by the expand amount. if you want them to expand by a factor of 2 you need to multiply( * ) by the expand amount.
That completely ruins the look of it. The script allows the objects to be separate out looking bigger as a whole but each object individually becomes smaller. Changing the division to multiplication overdoes the size by far if I want it to be in the same position. If there was a way to lock the size it'd be much better.
Answer by Berge · Apr 19, 2015 at 08:34 PM
If i understand you correctly, you want the childs of the parent to increase equally in size right? Then you should also use the ∗ operator inside the for loop to multiply by the expand ammount instead of /. BTW: You can also set the child size with ∗= or /= as you did with the parent.
Since i'm not allowed to post a reply: I don't know if the parent will affect the childrens scale or if they just get bigger without any values changing.
Try: Save the scale/size of the child as a variable (vector3) before changing the parents scale. You can then set the scale back to the original size with the saved variable.
Oh that was what I forgot to put in the post. I just want the objects to remain the same size.
Your answer
Follow this Question
Related Questions
Expand object on touch? 1 Answer
Problem creating a script to destroy object on collision. 1 Answer
Collision On Object Error. Please help. 1 Answer
Scaling Script? 1 Answer
Duplication Problem. Please help. 2 Answers