Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kiwi2703 · Feb 06, 2014 at 10:07 PM · javascripterrorstaticstatic-function

"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");
         }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nesis · Feb 07, 2014 at 12:15 AM 1
Share

Deleted my answer, this one is correct.

avatar image Kiwi2703 · Feb 07, 2014 at 01:43 PM 0
Share

I'm sorry, I actually fixed this problem already, but I forgot to delete the question. Thank you for responding though! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Scripting error #2! 2 Answers

CharacterController doesn't rotate on my script... 1 Answer

Loading next level, but next level never gets incremented. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges