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 simesf 1 · Apr 20, 2011 at 09:29 PM · staticmethod

Unable to access a static method from another class

I have a GameManager class that handles various operations during gameplay. I have a Ball class which contains the following static method:

public static void CheckAllBallsAreStill() { GameManager.allBallsAreStill = true; foreach(Ball ball in Ball.BallList) { if(ball.ballState != BallState.RestingBall)//if the ball is not at rest {

         if(!ball.isDead)//if the not-at-rest ball isn't dead
         {
             GameManager.allBallsAreStill = false;
         }
     }
 }

}

The GameManager class also has a reference to a Ball object - public GameObject Ball; at the top of the file. What I want to do each in each Update() is Access CheckAllBallsAreStill(). It stikes me that because I'm iterating through a list that a static method residing in the Ball class called once each click from my GameManager seems the most appropriate thing to do.

I've been on this site and the Unity scripting guide and tried various GetComponent methods within the GameManager class and I can access non-static methods & variables but not the static method. It's like I can only access an object instantiated from the Ball class but not the class itself. Any help would be much appreciated.

Comment
Add comment · Show 1
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 Bunny83 · Apr 20, 2011 at 10:23 PM 0
Share

Well, i don't see any problem with this method as long as (if Game$$anonymous$$anager is a class) allBallsAreStill and BallList are also static variables. Just another improvement (it won't help your actual problem but makes this method faster ;)): Since your condition is to check is any ball is not "resting" insert a break; after you set allBallsAreStill = false;. It's useless to continue the loop.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by flaviusxvii · Apr 20, 2011 at 10:00 PM

It looks to me like you should be able to call Ball.CheckAllBallsAreStill(). What does the compiler say?

Comment
Add comment · Show 3 · 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 simesf 1 · Apr 21, 2011 at 07:23 AM 0
Share

Thanks for getting back to me. If the compiler is saying anything I don't know where it is. I'm using $$anonymous$$onoDevelop. I type 'Ball.' inside Update() in my Game$$anonymous$$anager class and a list of autocomplete options turns up but there's no CheckAllBallsAreStill text. I've tried adding (amongst other statements) Ball ball = GetComponent(); and still no static function listed.

avatar image simesf 1 · Apr 21, 2011 at 09:03 AM 0
Share

Update: I've tried the following two constructs: Ball ball = gameObject.GetComponent("Ball") as Ball;

Ball ball2 = gameObject.GetComponent(); Both give me access to object variables and methods but not static varibles and methods. I thought it might be the GameObject Ball reference at the top of the Game$$anonymous$$anager class so I tried the same lines in a class that doesn't have the the reference to the GameObject Ball but no success yet.

avatar image simesf 1 · Apr 21, 2011 at 09:53 AM 0
Share

Update 2 - I'm getting some strange behaviour. I was able to access the method from my Level$$anonymous$$anager class and other classes, and for a short while I was even able to access CheckAllBallsAreStill from within my Game$$anonymous$$anager class, but now now. Things are now working but via my Level$$anonymous$$anager class, not Game$$anonymous$$anager class. I suspect it's something that I've done which is too involved to post here so I'll thank people here for their helpful insights - it helped - but I'll go back & look at my code.

avatar image
0

Answer by burgunfaust · Apr 20, 2011 at 10:31 PM

Maybe you are not accessing it just right from your game manager. Can we see the part of that script that deals with this?

Comment
Add comment · Show 1 · 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 Marnix · Apr 20, 2011 at 10:41 PM 1
Share

@burgunfaust: This is more a comment than a real answer...

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

How is this function parameter magically disappearing? 0 Answers

Why is my kill method not running the score system method call? 1 Answer

writing a utility (static) function properly 1 Answer

How to call class object from static function? 0 Answers

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 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