Component switching

Hey guys,I am getting the hang of javascript.

Right now,I am trying to make a component switching script,but I kept having errors.

Script:

#pragma strict

var Weapon01 : Component;
var Weapon02 : Component;

function Update () {
	if (Input.GetKeyDown("f"))
	{
		SwapWeapons();
	}
}

function SwapWeapons()
{
	if (Weapon01.active == true)
	{
		Weapon01.SetActiveRecursively(false);
		Weapon02.SetActiveRecursively(true);
	}
	else 
	{
		Weapon01.SetActiveRecursively(true);
		Weapon02.SetActiveRecursively(false);
	}
}

Please help

SetActiveRecursively being a memeber to GameObject, however
if you are using a unity version 4.x than GameObject.active and GameObject.SetActiveRecursively() have been removed.

You can use GameObject.activeSelf, GameObject.activeInHierarchy and GameObject.SetActive().

In order to activate the children, you have to iterate.

like this:

function SetActiveRecursively (go : GameObject, active : boolean) 
{
    go.SetActive (active);
    for (var t : Transform in go.transform) 
    {
        SetActiveRecursively (t.gameObject, active);
    }
}

As the errors says. SetActiveRecursively is not part of Component so you can not use it there. I think what you want is to either switch the weapon references to be of type GameObject (and use .SetActive) or to the class the weapons actually are (and use .enabled).

This tutorial shows how to enable/disable components. I would also recommend going through the other scripting tutorials as well: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn.

function SwapWeapons()
{

if (Weapon01.active == true)
{
   Weapon01.SetActive(false);
   Weapon02.SetActive(true);
}
else 
{
   Weapon01.SetActive(true);
   Weapon02.SetActive(false);
}

}