- Home /
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.
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.
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?
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.
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.
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.
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?
@burgunfaust: This is more a comment than a real answer...
Your answer
Follow this Question
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