- Home /
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;
}
}
Answer by Grinch91 · Mar 27, 2014 at 05:16 PM
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");
}
Answer by diegzumillo · Mar 26, 2014 at 01:55 PM
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.
I'm really struggling with this, I just can't get what I want to work to actually work. Basically a card is generated as a newObj and placed into the playerHand. What I want to do then is to take the newObj GameObject once it is clicked and move it into playerActive which is the play area for the player.
Basically my $$anonymous$$ouseController script is like this now:
public class $$anonymous$$ouseController : $$anonymous$$onoBehaviour {
public void On$$anonymous$$ouseDown()
{
Debug.Log ("$$anonymous$$ouse Down");
GameObject temp = new GameObject();
temp = this.gameObject;
if(temp == null)
{
Debug.Log ("Nothing found in the object you clicked");
}else
{
transform.parent.gameObject.GetComponent<Game>().OnCardHandClick(temp);
}
}
public void OnCardHandClick(GameObject obj)
{
Debug.Log ("You have clicked a card");
obj.transform.parent = playerActive.transform;
}
I think I am close to what I need to do but I keep getting the error of NullReferenceException: Object reference not set to an instance of an object. I know this error means that there is basically no information in the object or that it is null and can't do anything with it. But what I don't is how to make it so that when I click the object all of its data will be there. Due to the fact that this happens at runtime I can't drag in an instance into the inspect.
I don't know if this will help but maybe it will. Also I checked with some Debugs, it is getting inside the else statement so my problem must be the transform.parent. But again I really am confused as to what it should be.
Who has this script? the player? You wrote "this" there, it means the script on the player. So you are creating a new gameobj (temp) and assigning the current component to that gameobj... doesn't make much sense. You could add a new component to the newly created gameobj though. Is this what you're trying to do?
The script is within both Hand-Enemy and Hand-Player.
Yeah what you described sounds pretty much perfect. But I'm really confused about it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Raycast only works while moving 2 Answers
C# script for highlighting certain objects when the VR Controller is pointed at them? 0 Answers
Problem with Raycast 1 Answer