Script requires full declaration for it's own variables when called via another script

Hey there, I’m having a weird issue that is likely just a result of my shallow C# knowledge but here is what I’m experiencing.

I have two scripts, a UI interaction handler and then a character controller. When I click a button in the UI, it calls a function in the UI handler and that function’s only purpose is to call a function in the character controller.

The issue is that in the character controller, if I have Variable1, it doesn’t work when called from the UI handler. It will work if I declare it like this in the function though. CharacterController.Instance.Variable1. Is this to be expected? Am I calling something incorrectly? It looks weird declaring the class/instance in functions within the class.

Here’s a made up example. Just typing it up on the fly, not the actual code. Lines 25 and 26 are the area I’m having an isue with.

public class UiHandler : MonoBehaviour {

public void ShootWeapon() {

CharacterController.Instance.Shoot();

}
}


public class CharacterController : MonoBehaviour {

public CharacterController Instance;
protected float weaponAvailable = true;

protected void Awake() {
            if (Instance == null)  {  Instance = this; } else if (Instance != this) { Destroy(gameObject); }
}

public void Shoot() {

// shoot logic

weaponAvailable = false; // doesn't work
CharacterController.Instance.weaponAvailable = false // does work
}

}

It’s really impossible to tell what’s going on from your example. You should share the real code, and any actual error messages that you’re encountering.

Your example cxurrentlyt makes no sense as you’re trying to set a float to false for example.

“Instance” needs to be a static member.

public static CharacterController Instance;

It appears you used some singleton code and missed that keyword (arguably the most important keyword in the whole concept!)

For future posts - “doesn’t work” is not a useful phrase, please post the error messages you get.

1 Like

@PraetorBlue I probably should have so that random errors like that wouldn’t have made it into the example but the rest of the script isn’t relevant to this issue. It is a bool in the actual script, I started to put the weapon cooldown timer in there which is where the float came from but I figured that’d just complicate things.

@Ray_Sovranti good callout but that was just me failing to label it as static in the exmample. It is in the actual script. I agree “Doesn’t work” wasn’t the right way to explain it.

Debug.Log(weaponAvailable); // returns false;
Debug.Log(CharacterController.Instance.weaponAvailable); // returns true

It seems like the instance declaration is required within the method even though it was called as a method on the instance already. Is that the case?

That shouldn’t be required. The reason it’s not working probably has to do with some other code you haven’t shared in this thread thus far. For example if you have declared another field with the same name inside the method.

@PraetorBlue You’re right, we found the issue. In one of our scenes, someone hadn’t updated the script attached to the button to our UI handler script and it instead was still just referencing the character controller script, not an instance.

Also, I’m “someone”.

1 Like

Some days we are all “someone”

2 Likes