- Home /
"Failed to call static function because an object was provided" error?
Hello there :) I wrote a script that drains the battery life of my flashlight over time, lowers the flashlight's intensity and displays remaining seconds of flashlight's battery life on the screen. The script was working perfectly, but since I need to access and modify the battery charge from other scripts, I tried to modify the function to be static. Although I can get around writing some simple scripts, I still don't really understand the whole static functions and variables things, so, ultimately, I failed. I can play the game, but it continously spams the debug with error messages: "Failed to call static function Drain because an object was provided". I have no idea how to fix that, so I'm turning to this awesome community that has helped me few times already. Can you please help me with this as well? Thank you! :) The script is below:
static var batteryFull : float = 1000;
static var batteryCurrent : float = 1000;
static var flashlightFullInt : float = 4;
static var batteryDrainRate : float = flashlightFullInt/batteryFull;
static var batteryDrainSpeed : float = 0.120;
static var flashlightCurrentInt : float = 4;
static var batteryPercentage : float = flashlightCurrentInt * 25;
static var secondsToDrain : float = batteryDrainSpeed * batteryFull;
static var secondsToDrain2 : float = batteryDrainSpeed * batteryFull;
var Flashlight : GameObject;
static var myLight : Light;
var HUD : GameObject;
static var secondsLeftText : GUIText;
function Start(){
Flashlight = GameObject.Find("Flashlight");
FlashlightDrain.myLight = Flashlight.GetComponent("Light");
HUD = GameObject.Find("HUD");
FlashlightDrain.secondsLeftText = HUD.GetComponent("GUIText");
InvokeRepeating("Drain", 1, FlashlightDrain.batteryDrainSpeed);
}
static function Drain(){
if (PauseCheck.IsPaused == false &&
FlashlightDrain.myLight.enabled == true &&
FlashlightDrain.secondsToDrain > 0){
FlashlightDrain.flashlightCurrentInt = flashlightCurrentInt - batteryDrainRate;
FlashlightDrain.myLight.intensity = flashlightCurrentInt;
FlashlightDrain.batteryCurrent = flashlightCurrentInt * 250;
FlashlightDrain.secondsToDrain = batteryDrainSpeed * batteryCurrent;
FlashlightDrain.secondsToDrain2 = Mathf.Round(secondsToDrain);
secondsLeftText.text = secondsToDrain2.ToString();
Debug.Log("Draining");
}
}
Answer by AlkisFortuneFish · Feb 06, 2014 at 11:58 PM
You are trying to invoke a static function as a coroutine, via InvokeRepeating. This will not work.
Now, my question is, what exactly are you trying to achieve, why is everything static in this class? If you need unique static global access to this members, you should consider using the singleton pattern but even then, it's worth explaining the design you have in mind, we may be able to help.
I'm sorry, I actually fixed this problem already, but I forgot to delete the question. Thank you for responding though! :)