Accessing variables in children and parents.

Alright, I’ve been reading topics for such things and also have been reading the reference, but I can’t get that to work.

I’m making a weapon system (I don’t wanna use codes, that are already made by someone else). So in my parent I have a variable:

static var current_weapon : int = 0;

which is set to 1 or 2, depending on what key the player presses:

if (Input.GetKeyDown("1")) 
{
current_weapon = 1;
}	
	
if (Input.GetKeyDown("2")) 
{
current_weapon = 2;
}

Than in my children script (the weapons) I got this line:

print(transform.parent.GetComponent(CamControl_SP).current_weapon == weaponnumber);

the name of the parent’s script is “CamControl_SP”, the “weaponnumber” variable is an exposed variable, that I change from the editor, depending on what number I want the weapon to be.

The problem is that this code up there outputs to the console true and false…the current_weapon variable is integer, not boolean, why does it output true and false?

And another question:
How am I supposed to get a script’s variable’s value from the parent. I tried GetComponentInChildren(), but both of the children have the same component, how do I tell the script to get only the first child’s component?

print(transform.parent.GetComponent(CamControl_SP).current_weapon == weaponnumber);

Is a boolean operation.

== will always return true or false.

Here’s what you meant to type:

print(transform.parent.GetComponent(CamControl_SP).current_weapon);

Oh dude, I don’t know what to say…that was so dumb, I just copied that from an if statement and put it in a print command… completely forgot about the “==”. Sorry for posting such a stupid post.

But at least my second question wasn’t that stupid I hope.

This is what FindWithTag was meant for.