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;
}
}
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(). My 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.
– anon68636157What exactly happens when you make it public?
– WolframIt gives me these errors: An instance of type 'ScoreKeeper' is required to access non static member 'EndGame'.
– anon68636157How do you call EndGame()? You cannot use ScoreKeeper.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. Instead, as I suggested, use a Singleton (search for howto in the commulity), or find the object the ScoreKeeper script is attached to: GameObject.Find("ScoreKeeperObjectName").GetComponent("ScoreKeeper").EndGame();
– Wolfram