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
0
Question by Daev · Aug 06, 2010 at 09:54 PM · functionstaticprivatebce0020

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

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · 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 Daev · Aug 07, 2010 at 01:14 AM 0
Share

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.

avatar image Wolfram · Aug 07, 2010 at 01:41 AM 0
Share

What exactly happens when you make it public?

avatar image Daev · Aug 07, 2010 at 04:29 AM 0
Share

It gives me these errors:

An instance of type 'Score$$anonymous$$eeper' is required to access non static member 'EndGame'.

avatar image Wolfram · Aug 07, 2010 at 10:47 AM 0
Share

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();

avatar image
0

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.

Comment
Add comment · 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

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

No one has followed this question yet.

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


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