- Home /
Can't set an int from seperate script
Hi I'm making a weapon grouping system for a mechwarrior style game.
I want the system to set the weapons group to an integer of 1,2,3 or 4 by getting the int "GroupNum" and weapon "Pwep" from a UI button's "ButtonValue" script when clicked, then changing the weapon's int "WeaponGroup" with the "SetFor*TYPE*" function in weapon array.
.
The problem I am having is that when the "SetFor*TYPE*" function is called the the "GroupNum" int (which is set to the correct int from the "ButtonValue" script) returns a NullExceptionError and does not set the weapon's "GroupNum".
Button Value script
public class ButtonValue : MonoBehaviour {
ButtonValue thisButton;
public bool projectile;
UIPWeaponValue Pwep;
void Start () {
thisButton = this.GetComponent<ButtonValue>();
//gets the weapon value of selected weapon if the bool is set to that type
if (projectile == true) {
Pwep = transform.GetComponentInParent<UIPWeaponValue>();
}
}
public void SetGroup() {
FindObjectOfType<WeaponArray>().button = thisButton;
//sets the selected weapons and calls the relevent fuction in weaponArray
if (Pwep !=null) {
FindObjectOfType<WeaponArray>().weapon = Pwep.gameObject;
FindObjectOfType<WeaponArray>().SetForProjectile();
}
}
} Weapon Array Script
public class WeaponArray : MonoBehaviour {
List<int> WeaponGroup = new List<int>();
public ButtonValue button;
public GameObject weapon;
void Start () {
WeaponGroup.Add(1);
WeaponGroup.Add(2);
WeaponGroup.Add(3);
WeaponGroup.Add(4);
}
public void SetForProjectile() {
print(button.GroupNum);
weapon.GetComponent<WeaponProjectileScript>().weaponGroup = button.GroupNum;
}
It seems like the script cant find the weapons "weaponGroup" int but I dont know why, the "weapon" group int is a public int .
Answer by Omerl_ahire · Feb 16, 2018 at 03:43 AM
Have fixed the problem, I was getting the wrong object in the "SetGroup" function so it was getting the button value instead of the weapon value I changes that and now it works fine.