Scaling objects in C#

I would like to be able to make an object smaller by clicking on it with the left mouse button and bigger with right mouse button. The problems with this code are:

  1. it doesn’t allow to scale down unless its been scaled up
  2. if there are multiple objs, they all get affected once they’ve been clicked on.
    I’ve tried a few different ways to do it and I’m guessing it’s something to do with the resize bool. Your help is much appreciated

using UnityEngine;
using System.Collections;

public class Scale : MonoBehaviour
{
public GameObject obj;

private float targetScale;
public float maxScale = 10.0f;
public float minScale = 2.0f;

public float shrinkSpeed = 1.0f;

private bool resizing = false;

void OnMouseDown()
{
resizing = true;
}

void Update()
{
	if (resizing)
	{
		 if (Input.GetMouseButtonDown(1)) 
		{
			targetScale = maxScale;
			//Resize();
			
		}
		 if (Input.GetMouseButtonDown(0))
		{
			targetScale = minScale;
			//Resize();
			
		}
		
		obj.transform.localScale = Vector3.Lerp(obj.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
		
		Debug.Log(obj.transform.localScale);
		
		if (obj.transform.localScale.x == targetScale)
		{
		resizing = false;
			Debug.Log(resizing);
		}
}
}

}

OnMouseDown() only captures a left button click, so you right button click was only working if ‘resizing’ had already been set to true by a left button click. To solve this problem, you’ll have to move to a raycast model. Below is a bit of code implementing a 3rd party raycast model. That is, the script only has to exist in one place, not on every object. But the one drawback is that if you click on a second object before the first one has completed growing/shrinking, the growing/shrinking of the first object stops. I’m not sure of your purpose or need, so consider this example code:

using UnityEngine;
using System.Collections;

public class Scale : MonoBehaviour {
	
private Transform tr;

	public float maxScale = 10.0f;
	public float minScale = 2.0f;
	public float shrinkSpeed = 1.0f;	
	
	private float targetScale;
	private Vector3 v3Scale;
		
	
	void Update()
	{
		RaycastHit hit;
		Ray ray;
			
		if (Input.GetMouseButtonDown (0)) {
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(ray, out hit)) {
				tr = hit.transform;
				targetScale = minScale;
				v3Scale = new Vector3(targetScale, targetScale, targetScale);
			}
		}
			
		if (Input.GetMouseButtonDown (1)) {
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(ray, out hit)) {
				tr = hit.transform;
				targetScale = maxScale;
				v3Scale = new Vector3(targetScale, targetScale, targetScale);
			}
		}
	 
		if (tr != null)
	 	   tr.localScale = Vector3.Lerp(tr.localScale, v3Scale, Time.deltaTime*shrinkSpeed);
	}
}

private Vector3 targetScale;
public float maxScale = 10.0f;
public float minScale = 2.0f;
public float shrinkSpeed = 1.0f;
void Update(){
if (Input.GetMouseButtonDown(1))
targetScale.Set(maxScale,maxScale,maxScale);
if (Input.GetMouseButtonDown(0))
targetScale.Set(minScale,minScale,minScale);
if (transform.localScale != targetScale)
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime*shrinkSpeed);
}

Hope this helps =)