Trying to make a script to transform an object when I click on it

Hi, I’m pretty new to Unity and C# and am just messing around for fun. I tried to rewrite a script which I’m using to pickup an item, so instead an item I press on changes scale. I’ve tried to run the script but it doesn’t seem to work, here it is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeSizeScript : MonoBehaviour
{
    public GameObject player;
    public float clickRange = 5f; //how far the player can pickup the object from

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0)) //capture input
        {
             //perform raycast to check if player is looking at object within pickuprange
            RaycastHit hit;
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, clickRange))
            {
                //make sure click tag is attached
                if (hit.transform.gameObject.tag == "canClick")
                {
                    //pass in object hit into the ChangeSize function
                    ChangeSize(hit.transform.gameObject);
                }
            }
        }

    }

    void ChangeSize(GameObject changesizeObj)
    {
        changesizeObj.transform.localScale += new Vector3(5, 5, 5); //change size
    }

}


Any help would be much appreaciated

1 Like

I didn’t know that (Input.GetKeyDown()) can be used with Mouse.

Try to use Input.GetMouseButtonDown(0) instead:

1 Like