Moving GameObject Towards Spot when clicking a GUI Function

Hello Unity PPl I’m still trying to make an Awesome Card game here, but dang it’s so hard!

So I was wondering. Say I had a card in my hand and I want to summon it onto my field.

Alright so in my game when I click a card, I get this GUI Button Function named “Summon”

When I click Summon it’ll move the card towards a position on the field Zone.

Here’s the problem…

My Summon Button Script isn’t attached to every single Card GameObject.
It used to be like that but when I had it like that, whenever I clicked a gameobject that is tagged as card. It’ll show the Button “Summon” Multiplied times the number of cards that are in the Game World.

Right now my Summon Button script is in an Empty GameObject.

So what I need help with is… How do I make the Game know that I clicked that certain card that I want to summon?

Like say I clicked a card named “Knighlimphic” and the Summon button appears over it. and I click the Summon button.

Since my script isn’t attached to the card and it’s attached to an Empty GameObject. How will it know that I clicked, Knighlimphic as the card I want to summon?

Here’s what my code looks like. In the EmptyGameObject.

The main part of the Code is in the GUI Function area and below that. The other part of the code just tells me which spot is occupied.

Special Thanks to: Robertbu ( He gave me this GUI Script )

private var showButton = false;
private var pos : Vector2;
private var hit : RaycastHit;

function Update(){
	if (Mathf.Approximately(this.transform.position.x, Zone1 )){
Debug.Log("Monster Summoned in MonsterZone 1");
	Summon = false;
	Occupied1.active = true;
}
else{
	Occupied1.active = false;
}

	if (Mathf.Approximately(this.transform.position.x, Zone2 )){
Debug.Log("Monster Summoned in MonsterZone 2");
	Summon = false;
	Occupied2.active = true;
}
else{
	Occupied2.active = false;
}	

	if (Mathf.Approximately(this.transform.position.x, Zone3 )){
Debug.Log("Monster Summoned in MonsterZone 3");
	Occupied3.active = true;
	Summon = false;
}
else{
	Occupied3.active = false;
}


	if (Mathf.Approximately(this.transform.position.x, Zone4 )){
Debug.Log("Monster Summoned in MonsterZone 4");
	Occupied4.active = true;
	Summon = false;
}
else{
	Occupied4.active = false;
}




	if (Mathf.Approximately(this.transform.position.x, Zone5 )){
Debug.Log("Monster Summoned in MonsterZone 5");
	Occupied5.active = true;
	Summon = false;
}
else{
	Occupied5.active = false;
}
}

function OnGUI() {
    var e = Event.current;
 
    var x = pos.x - 50;  // Calc x and y to center of button
    var y = pos.y - 10;

    if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
        Summon = true;
        showButton = false;
    } else {
       if (e.type == EventType.MouseDown) {
         CheckClick();
       }
    }
    
}
 
function CheckClick() {
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
    if (Physics.Raycast(ray, hit)) {
       if (hit.collider.tag == "Card") {
         pos = Camera.main.WorldToScreenPoint(hit.transform.position);
         pos.y = Screen.height - pos.y;  // convert from Screen to GUI
         showButton = true;
       }
    }
    else {
       showButton = false;
    }
}

Please help me out. I don’t like being stuck on one thing for too long lol.
I’d really appreciate it.

On top of your script, next to your other vars:

   private var selectedCard : GameObject;

Try this on your Update()

	if (Input.GetMouseButtonDown (0) && !showButton)
	{
 	    CheckClick ();
	}
	if (Summon)
	{
		selectedCard.transform.position = Vector3.MoveTowards(transform.position, target1.position, step);
	}

Your OnGUI() should be:

function OnGUI() 
{
    var e = Event.current;
 
    var x = pos.x - 50;  // Calc x and y to center of button
    var y = pos.y - 10;
 
	if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) 
	{
	    Summon = true;
	    showButton = false;
	}
}

Your CheckClick()

function CheckClick() 
{
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
    if (Physics.Raycast(ray, hit)) 
    {
       if (hit.collider.tag == "Card") 
       {
         selectedCard = hit.collider.gameObject;
         pos = Camera.main.WorldToScreenPoint(hit.transform.position);
         pos.y = Screen.height - pos.y;  // convert from Screen to GUI
         showButton = true;
       }
    }
    else 
    {
       showButton = false;
    }
}