Cant figure out how to activate and deactivate objects on multiple characters

sorry dont know why it didnt get the top part of the script in.
Anyhow this script is to assign villagers diff jobs in the my game. Now everytime you activate a job it will
activate the proper tool for the job. The problem im having is once i add more than one villager all with the same script it only sets the items active and inactive on one villager. Can anyone please help me.

void Awake()
{
    stonepick = GameObject.FindGameObjectWithTag("StonePick");
    axeStone = GameObject.FindGameObjectWithTag("StoneAxe");
    basket = GameObject.FindGameObjectWithTag("Basket");
    wod = GetComponent<WoodCutter>();
    gat = GetComponent<Gatherer>();
    iron = GetComponent<IronMiner>();
    con = GetComponent<Builder>();
    

     WoodCutter = false;
     Miner = false;
     Gatherer = false;
     Hunter = false;
     Fisher = false;
     Farmer = false;
     builder = false;
     Smith = false;
     Doctor = false;
     Priest = false;
     Armorer = false;
     Knight = false;
     Scout = false;
     Archer = false;
     Infantry = false;
     Leader= false;
     KingsGuard = false;
     Cook = false;

}

void Start()
{
    gat.enabled = false;
    wod.enabled = false;
    iron.enabled = false;
    con.enabled = false;
    stonepick.SetActive(false);
    basket.SetActive(false);
    axeStone.SetActive(false);
}

void Update()
{
    if(Gatherer == true)
    {
        gat.enabled = true;
        basket.SetActive(true);
    }

    if (Gatherer == false)
    {
        gat.enabled = false;
        basket.SetActive(false);

    }
    if (WoodCutter == true)
    {
        wod.enabled = true;
        axeStone.SetActive(true);
    }

    if (WoodCutter == false)
    {
        wod.enabled = false;
        axeStone.SetActive(false);
    }
    if (Miner == true)
    {
        iron.enabled = true;
        stonepick.SetActive(true);
    }
    if (Miner == false)
    {
        iron.enabled = false;
        stonepick.SetActive(false);
    }
    if (builder == true)
    {
        con.enabled = true;
    }
    if (builder == false)
    {
        con.enabled = false;
    }
}

}

I am unsure why you are using a long sequence of bools for your jobs. To prevent having two jobs at once, you could use an enum… Would probably be easier to script as well :wink:

The problem you are facing, if I understand it right, is that you would like each villager to have either a Stonepick, stoneaxe or a basket, depending on their job.

Note that you are using GameObject.FindGameObjectWithTag("Stonepick") . This would be fine if you only had one character. Unfortunately, FindGameObjectWithTag being a static function, and not directly linked to the character the script is attached to, the result of calling this function will always be the same, wherever you call it from. So Whatever character the script is linked to, the Stonepick will always be the first one found using this method.

I assume the tools are children of the character game object. You need to find the objects among the object’s children.

To do this:

void Awake()
 {
     foreach (Transform t in transform)
     {
         if(t.gameObject.Tag == "Stonepick") stonepick = t.gameObject;
         else if(t.gameObject.Tag == "Stoneaxe") stoneaxe = t.gameObject;
         else if(t.gameObject.Tag == "Basket") basket = t.gameObject;
     ...
     }
}

I hope this helps :slight_smile: