Home > Mobile >  Send int from second script to bool[list] from first script
Send int from second script to bool[list] from first script

Time:07-23

How can I achieve Bool[int] for current weapon type identification?

WeaponController

public class WeaponController : MonoBehaviour
{
public bool[] CurrentWeapon = new bool[2];

public bool Wrench;
public bool Pistol;

void Start()
{
    Wrench = CurrentWeapon[0];
    Pistol = CurrentWeapon[1];
}
}

WeaponSwitching

public class WeaponController : MonoBehaviour
{
public int selectedWeapon = 0;

void Update()
{
    Player.GetComponent<WeaponController>().CurrentWeapon.bool[Paste here] = selectedWeapon;
}
}

CodePudding user response:

In your WeaponSwitching class, change CurrentWeapon.bool[Paste here] = selectedWeapon; to CurrentWeapon[index] = value;

You'll need to change index to the array index you want to change and value to a boolean value. Currently you are using the int selectedWeapon which will become true if it's equal to 1 and false if it's equal to 0.

It also looks like your code may have a few other issues

  1. Both classes say public class WeaponController. They need to have different names.
  2. A Wrench and Pistol boolean are being assigned on start, but won't do anything afterwards if they don't have more code.

An easier way to do this might be to use a public int CurrentWeaponin the WeaponController class instead of bool and change that from the WeaponSwitching class.

Here is a similar question with someone having a similar problem

  • Related