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;
}
}