public class CardsInHand : MonoBehaviour {
public int SelfCardPositionInHand = 0;
GameObject[] TEST;
void UpdateSelfPos(){
TEST = GameObject.FindGameObjectsWithTag("Card");
foreach (GameObject card in TEST){
if (gameObject.GetComponent("CardsInHand") as CardsInHand !=null){
card.gameObject.GetComponent<CardsInHand> ().SelfCardPositionInHand = ((card.gameObject.GetComponent<CardsInHand> ().SelfCardPositionInHand) - 1);
//gameObject.GetComponent<CardsInHand> ().SelfCardPositionInHand = (gameObject.GetComponent<CardsInHand> ().SelfCardPositionInHand - 1);
}
}
}
hi im having trouble understanding and getting this to work.
EDIT: what i am hoping to do is - 1 from an int variable on all game objects that have this script attached. where the int variable is different on every game object, is this generally the correct direction to be going in?
I am not sure what the “card” in this line : foreach (GameObject card in TEST){ ; represents is it the name of the instanced game objects added to TEST or is it something thats being created by this line of code, very confused :)(
thanks
First of all, do yourself a favor and break every single step in line 9 into separate statements, one after the other.
Obviously you’ll need braces after your if() statement to enclose all the statements you’re going to break it into.
With line 9 written the way it is it is not possible to ever know what portion is choking.
If you do the steps one at a time, you will soon find that (perhaps) the object in question has no CardsInHand component, for instance.
1 Like
Hi, thanks for the reply, i will work on that.
what i am hoping to do is - 1 from an int variable on all game objects that have this script attached. where the int variable is different on every game object, is this generally the correct direction to be going in?
still very much stuck and banging my head against the desk… anyone have any ideas?
Looking closer I see that line 12 asks for components on gameObject, which is a shortcut where this script is running.
Then line 13 does component-getting stuff against card, which is the individual card.
I think that disparity could be your entire problem?
yes i eventualy came to that conclusion also and removed the entire if statement ie line 12. completely. I then get a error :
NullReferenceException: Object reference not set to an instance of an object
trying to figure out why this occurs.
Break that line into something that does these steps:
- gets the CardsInHand component off the card object
- checks if it is null
- if it is not null then subtract 1 from that .Selfcardwhatever property