# Resetting The object scale to it's original size!

I have this sprite renderer in unity. I have made so that it increases it's scale. The Object is:

This object is a child of empty gameObject and I have resized the parent gameObject rather than this one and added the script to parent rather than this object. The code I have written is:

Scaler = transform.localScale;
Scaler.x += 3f;
transform.localScale = Scaler;
if(Scaler.x > MaximumPoint){
Scaler.x -= 3f;
transform.localScale = Scaler;
}


This code doesn't work because if the object decrease by 3f, it isn't greater than MaximumPoint anymore. So, this code stops.

Another Code , I have tried is:

void OnCollisionTrigger2D(Collision2D col){
if(col.gameObject.name == "Collider"){
Scaler --;
}
}


This one throws a null pointer exception because I am not sure why?

Another thing I have tried is raycasting but the ray doesn't move with the increased scaled object.

• Could you make a variable that stores the original scale in Awake() or Start() and then set it back to that scale when you need it to by using that variable instead of the 'magic number' 3f?. Also, you say you'd like to scale the object size, but you seem to be changing transform.localPosition, which of course will change its position in the scene and not the scale/size – Big T Larrity Jan 5 at 15:18
• you can try transform.localScale instead of localPosition – Big T Larrity Jan 5 at 15:25
• @BigTLarrity, Sorry, It is localScale . I wrote it localPosition. – Sabin Pyakurel Jan 5 at 15:27
• @BigTLarrity, It was my mistake that I wrote the position instead of scale in question. But I don't understand, what changes will it make if I just replace 3f to a floating variable?? – Sabin Pyakurel Jan 5 at 15:29
• it was something to try only. I don't know exactly what Scaler is even from your code to be honest (I''m not expert, was only tryingto quickly help). But lets say you just set transform.localScale directly instead of via Scaler, you could have a variable called originalScale = 1f and one called largeScale = 4f for example, and then just set it directly to see if your code is working. I mainly work on 3d so maybe there is something for Unity 2D that I am missing. Also i think the null exception is due to the fact you are trying to decrease 'Scaler' and not 'Scaler.x' – Big T Larrity Jan 5 at 15:38

Why don't you just use a bool that determines whether you're increasing the size or decreasing? Could use something like this:

bool increasing = true;
Vector3 Scaler;
float maximumX;
float minimunX;

void Start()
{
minimunX = transform.localScale.x;
maximumX = "Whatever you want the max to be";
Scaler = transform.localScale;

}
public void DoNothing()
{
if(Scaler.x >= maximumX)
{
increasing = false;
}
else if(Scaler.x <= minimunX)
{
increasing = true;
}

if (increasing)
{
Scaler.x += 3f;
}
if (!increasing)
{
Scaler.x -= 3f;
}

transform.localScale = Scaler;
}


Hope this helps :)