Selecting an Object

Ok so I am a little unsure with a few things about selecting an Object. So what I am trying to do is when you click on an object from one parent, provided the resources are there it will move the object, to a new parent. So for example in my game you click on Card A from Player’s Hand, it checks resources and then either moves it to Player’s battlefield or nothing if resources aren’t there.

I am looking at Raycasting currently and my main question is how do I actually move the object, from one parent to the other all information remaining too? This is what I am trying but doesn’t seem to be working.

public class MouseController : MonoBehaviour {

	GameObject storeObj = new GameObject();

	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hit;

	public void OnMouseDown()
	{
		Debug.Log ("Mouse Down");
		if(hit.collider != null)
		{			
			storeObj = hit.collider.gameObject;
			transform.parent.gameObject.GetComponent<Game>().OnCardHandClick(storeObj);
		}
	}
}

Also it is throwing an error from my function that creates the objects. Saying I cannot call this function when declaring a variable. Which I am confused about.

	void AddToPlayerHand()
	{
		CardDef c1 = playerDeck.Deal();
		if(c1 != null)
		{
			GameObject newObj = new GameObject();
			newObj.name = "Card";
			CardAttributes newCard = newObj.AddComponent<CardAttributes>();
			BoxCollider bc = newObj.AddComponent<BoxCollider>();
			bc.size = new Vector3(tempX,tempY,1);
			SpriteRenderer ren = newObj.AddComponent<SpriteRenderer>();
			string store = "battleChariot";//playerDeck.deck.CardDef.Back;
			Sprite mySprite = Resources.Load<Sprite>(store)as Sprite;
			newCard.Data = c1;
			newObj.transform.parent = playerHand.transform;
			float x = -6+(playerDeck.deck.Count)*2.0f;
			float y = -5.8f;
			ren.sprite = mySprite;
			Vector2 pos = new Vector2(x,y);
//This line is where it is pointing me to with the error			
			newObj.AddComponent<MouseController>();
			newObj.transform.position = pos;

		}

	}

To change the parent of something simply change the variable transform.parent. You can assign any gameobject to that variable.

Raycasting is one way to go, if you don’t have the card/gameobject referenced in your script. It depends on how your game is structured. If this is a card game, I would keep all the player’s cards as a big array on his script and simply cycle through that array to perform actions.

Ended up solving this.

private Player playerTemp;
	private GameObject gameInstance;

	void OnMouseDown()
	{
		playerTemp = new Player();
		temp = gameObject.GetComponent<CardAttributes>().Data.Name;
		gameInstance = GameObject.Find("Game");
		Debug.Log ("Mouse Down");
		if(playerTemp.playerSupply >= gameObject.GetComponent<CardAttributes>().Data.Cost)
		{
			Debug.Log ("Yay");
			Debug.Log ("Temp contains");
			Debug.Log(temp);
			gameInstance.GetComponent<Game>().OnCardHandClick(temp);
			Destroy(this.gameObject);
		}
		else
		{
			Debug.Log ("Nay");
		}