- Home /
Can't EndGame from another Script: problems between PRIVATE and STATIC functions
okay i had this problem where I had attached the scorekeeper to my gui to track the temperature in the game and then end the game when the temp got too high. I realized I couldn't or shouldn't attach the scorekeeper to my gui because then it wouldn't know how to access the animations for the Player when the game ended and I ended with instance type errors.
So I made a temperatureGauge function to track the temperature and attached the ScoreKeeper to my character.
However, the EndGame() function is a private function. So when I try to send the command from my temperatureGauge function that it's time to endGame and play the appropriate end game animations, it gives me errors because it's not a static function I'm trying to access.
When I make it a static function I get these errors that I typically have trouble debugging because I don't understand enough what they are trying to tell me.
ERRORS
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(167,57): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(167,57): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(168,47): BCE0020: An instance of type 'ScoreKeeper' is required to access non static member 'guiMessage'.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(178,9): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'SendMessage'.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(178,9): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'SendMessage'.
FUNCTION IN QUESTION:
static function EndGame() {
var animationController : AnimationController = GetComponent( AnimationController ); var prefab : GameObject = Instantiate(guiMessage); var endMessage : GUIText = prefab.GetComponent( GUIText );
endMessage.text = "Out of time!";
//audio.PlayOneShot(loseSound); animationController.animationTarget.Play( "LOSE" );
// Alert other components on this GameObject that the game has ended SendMessage( "OnEndGame" ); yield WaitForSeconds(2.5); Application.LoadLevel("GameOver");
while( true ) { // Wait for a touch before reloading the intro level yield WaitForFixedUpdate(); if ( iPhoneInput.touchCount > 0 && iPhoneInput.GetTouch( 0 ).phase == iPhoneTouchPhase.Began ) break; }
}
Answer by Wolfram · Aug 06, 2010 at 11:22 PM
Static functions are not actually attached to GameObjects, they exist globally. So you can't use "GetComponent()" with them. Why do you want this function to be static? Either make it public, or use a Singleton to make sure you have exactly one instance of the class that contains EndGame() while being able to reference other scripts (and var's like guiMessage) of the GameObject where you attached your ScoreKeeper script to.
As I understood it thus far, static is akin to global in other programs. The idea being that I have a function attached to a gui, which tracks the temperature and then when it gets too hot, makes a call to endGame().
$$anonymous$$y script is based on the tutorial from Penelope in fact it's real close, so I don't understand why it wouldn't work except that I'm calling it from another function script and that's when it breaks down, as soon as I make it static or public I get the errors associated with the animationController.
It gives me these errors:
An instance of type 'Score$$anonymous$$eeper' is required to access non static member 'EndGame'.
How do you call EndGame()? You cannot use Score$$anonymous$$eeper.EndGame() - that references the class name and can only be used for static functions, but it is much more work to convert EndGame() to a static function. Ins$$anonymous$$d, as I suggested, use a Singleton (search for howto in the commulity), or find the object the Score$$anonymous$$eeper script is attached to: GameObject.Find("Score$$anonymous$$eeperObjectName").GetComponent("Score$$anonymous$$eeper").EndGame();
Answer by Daev · Aug 08, 2010 at 06:39 AM
okay here is the actual answer. I figured it out.
First I realized that scorekeeper had to be attached to my character to make animations play, of course.
Then I realized that I could put my temperature tracking functions into scorekeeper and simply create a variable at the top just like for a timer like this:
var timerGui : GUIText; var temperatureGui : GUITexture ;
Lower down in the script are all the functions that control my temperature and the above variable shows up in my scorekeeper. The scorekeeper is attached to my player at this point. Finally, I grab the gui that I want to attach to this in the hierarchy and drop it in the expected slot I've created in my scorekeeper (this is in the software).
Note I was making some references to my gui previously that were not working, instead of calling it GuiTexture I made it temperatureGui which didn't conflict with the name of the node I added to the game to make the gui to begin with (GUITexture), and was just plain confusing.
Thanks to everyone for feedback and for questioning my approaches until I pulled it together. The best route for me was to simply print out my scripts lay them out on the floor and then cut them out like a puzzle and fit them together. Which is how I used to edit term papers back in the day before we had personal computers.
Your answer

Follow this Question
Related Questions
Accessing non static members in a static function argument?? 1 Answer
Simple static function to display text onscreen (Accessible from anywhere) 1 Answer
Member variables returning to 0 0 Answers
How can I make the animation events window stop showing private functions? 0 Answers
Find 2D Texture via Script to use in Static Function 2 Answers