how to use Mouse Click in Selecting Objects and deselecting. C#

Hi My name is paolo Ive been searching for this kind of script but it seems like no one gives answer. for the reason that some Pros who dont bother explaining stuffs will just say go read Unity Scripting Documentation. well probably it would help but I hope you can give me a working script to start studying about I’m still a newbie and I need a little bit of a elaboration consider me as a total Noob.

My problem is that I want to select an object in gameplay using mouseclick. I want the object follow the mouse cursor when its click and when I click it again it will not follow the cursor.

pretty much like in an inventory system where you click on an item and it follows the cursor so if you want to drop the item you need to click it again. But for now I’m not asking for the whole inventory System all I want to know is how am I goin to do that in a single object.

thanks in advance! hope you guys can help me.

Check the OnMouseOver function in the reference the make an array or variable to hold the gameObject and assign the object you clicked on to that variable, then tell the variable to follow the mouse position.
Search for mouse position in the reference.
Probably will look something like,

var obj:GameObject; // or Transform
var mousePos:Vector3;
function OnMouseOver ()
{     if(Input.GetButtonDown(0))
          obj=collider.gameObject;
}
function Update ()
{    mousePos = Input.mousePosition;
     obj.transform.position = mousePos;
}

didn’t test this, and to be honest if you want it to follow correct, you should also use transform lookat so the following object always looks at the object to follow and also create an offset from the mouseposition so that the follower doesn try to take the mousePos exactly

edit oeps used js instead of C#, change function to void and put the type of var before giving it a name…

Oh you might have miss understood the problem pro. Have you seen an inventory like system from an online game like RF online or others? you can see there that when you click an item it follows and takes up the mouse position everytime. it gives you the ability to click on an object and the object will follow your cursor as if you were actually holding it. then when you want to drop it anywhere inside your inventory, you click it in the position you want then the object stays on that position and will no longer follow the mouse cursor. this one doesn’t work, doesn’t help. Anyways thanks for explaining to me to just look at the reference.

Ah ok well you are make a 2d inventory in the GUI or a 3d one in worldspace? Anyway try starting with the ray from screenspace on mouseposition.
Look for mouseposition in the reference i believe theres an example in there of what you want.

What you probably need is a grid in GUI where you can drag and drop items?