- Home /
Help with Boolean not being recognized as true
I am creating a gun script that is only functional when the player has the gun equipped, Now that part works the player can pick up, equipt or unequipt and even drop the weapon and everything works fine Except when the gun is equipped i have a boolean set to true to enable the gun to be shot. However, even when the boolean in the inspector is showing true at runtime the script still passes it as false. Here is the snippet of the programs used with the boolean:
void Update()
{
//Debug.Log("Update : Start");
//rifle ability
//scoping
if (Input.GetKeyDown(KeyCode.Mouse1))
{
Debug.Log("Aimed");
if (rifleActive == true)
{
Debug.Log("rifle active true");
scoped = !scoped;
Scoping();
//shooting
if (Input.GetKeyDown(KeyCode.Mouse0) && Time.time > nextFire && inventory.inventoryEnabled == false)
{
Debug.Log("Pulled Trigger");
Shoot();
}
}
else
{
if(rifleActive == false)
Debug.Log("rifle active false");
}
}
this is midway through the update function so there is no closing bracket that is not an error. Here is the separate script (set on a different gameobject) that enables/disables the boolean:
public void Update()
{
if (equipped && type == "Axe")
{
player.GetComponent<PlayerAttributes>().axeEquipped = true;
}
else
{
player.GetComponent<PlayerAttributes>().axeEquipped = false;
}
if(equipped && type == "IcePick")
{
player.GetComponent<PlayerAttributes>().pickEquipped = true;
}
else
{
player.GetComponent<PlayerAttributes>().pickEquipped = false;
}
if (equipped && type == "Rifle")
{
player.GetComponent<PlayerAttributes>().rifleActive = true;
}
else
{
player.GetComponent<PlayerAttributes>().rifleActive = false;
}
}
The boolean in Question is rifleActive Here is a screenshot to show at runtime the boolean is set to true yet the debug.log in the else rifleActive == false is what is showing in console and the gun obviously neither aims nor shoots:
thank you for your assistance and time.
Didn't really answer your post but here i simplified your scripts:
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1)){
Debug.Log("Aimed");
if (rifleActive)
{
Debug.Log("rifle is active");
scoped = true;
Scoping();
//shooting
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0) && Time.time > nextFire && !inventory.inventoryEnabled)
{
Debug.Log("Pulled Trigger");
Shoot();
}
else{
Debug.Log("rifle is not active");
}
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse1)){
Debug.Log("Not Ai$$anonymous$$g");
scoped = false;
}
}
Other script:
public void Update()
{
if(equipped){
PlayerAttributes playerAtr = player.GetComponent<PlayerAttributes>();
playerAtr .axedEquipped = type == "Axe";
playerAtr .pickEquipped= type == "IcePick";
playerAtr .rifleActive = type == "Rifle";
Debug.log("Axe: " + playerAtr .axedEquipped);
Debug.log("IcePick: " + playerAtr .pickEquipped);
Debug.log("Rifle: " + playerAtr .rifleActive );
}
}
okay thanks anyways lol looks way cleaner lol
Okay so, after implementing the second script which you made the ai$$anonymous$$g now functions properly so thanks, I'm going to implement the first one now and see what happens
Answer by badadam · Jan 12, 2019 at 08:54 PM
if (equipped && type == "Rifle")
{
player.GetComponent<PlayerAttributes>().rifleActive = true;
}
else
{
player.GetComponent<PlayerAttributes>().rifleActive = false;
}
equipped value is false or type value is not "Rifle" you set rifleActive true on run time and milliseconds are passed and run else command. So rifleActive value is be set false by update method. And you can't see the result of setting rifleActive value true.
Answer by VenomDestroyer · Jan 12, 2019 at 09:03 PM
The equipped value and type are correct but I have been going through and rewriting the script and it is slowly starting to work one piece at a time so I think it was just bad logic on my part between different scripts. Thank you all for the input.