I’m trying to read a int from a object using 2D colliders, and then apply a specific action if that int is equal to a specific number, unfortunately, not working…
if (col.collider.gameObject.GetComponent<PlayerControllerHeli>().curPWeapon == 1) {
col.collider.gameObject.GetComponent<PlayerControllerHeli>().gun.FireRateManager(+value);
}
any suggestions?
This isn’t so much an answer to your question as an answer to the question’s title. As others have said, it isn’t clear exactly what you want to do? I just want to throw in my 2 pence on the matter. You should try not to get a component each time you want it, and instead get it once and reuse that. It’s good practice to check if the component was successfully found before using it. After these two steps you can do as you please with the component as shown below. I’m assuming curPWeapon is an int.
// Get the component only once
PlayerControllerHeli temp = col.GetComponent<PlayerControllerHeli>();
// Check we have the component we need
if(temp != null)
{
// Access anything in "temp" as type PlayerControllerHeli
if (temp.curPWeapon == 1)
{
// Do any action you like
temp.gun.FireRateManager();
}
}
You’ll notice I took out the +value parameter as I have no idea what you were trying to do with that. I hope this helps a little =D