- Home /
Calling function from separate script gets buggy
Hello, I have a super weird problem that I have only found a few other people having. The pages I have found of others having the problem didn't get answered. Some were even lead to believe it was corrupt.
My problem is I have a set of functions that freeze specific objects in the scene for the set amount of time, then unfreezes them. I have a check in the same script as the functions, where anytime i press the "K" key, it calls the function. It works perfectly. It calls it, freezes them, and after the specified time, it unfreezes them. BUT when I call that EXACT same function from a separate script it doesn't work, not entirely anyway. It will call the function, it freezes the objects, sets the timer, but for some really weird reason, doesn't count down. Instead they just stay frozen forever. Here is the code, any help would be awesome. Thanks. This is the GameControllerScript. It has the functions and keypress function call.
function Update{
if (Input.GetKeyDown(KeyCode.K)){
FreezeBallsForTime(5);
}
if (Level.freeze){
if (ballsFreezeTimer > 0){
ballsFreezeTimer -= Time.deltaTime;
if (ballsFreezeTimer <= 0){
Level.UnFreezeBalls();
}
}
}
}
function FreezeBallsForTime( waitTime : float ){
if (Level.freeze == false){
Level.FreezeBalls();
Debug.Log("ballsFreezeTimer " + ballsFreezeTimer);
ballsFreezeTimer = 5;//waitTime;
}else if (Level.freeze){
ballsFreezeTimer += waitTime;
}
}
public class Level
{
public static var freeze : boolean = false;
public static function FreezeBalls(){
if (freeze == false){
Debug.Log("FreezeBalls() Called");
freeze = true;
}
}
public static function UnFreezeBalls (){
Debug.Log("UnFreezeBalls() called");
freeze = false;
}
}
And then in the player script
function ActivateFreeze( time : float ){
gameControllerScript.FreezeBallsForTime(time); //This is referencing the first script. It will call the script fine, and it freezes the balls, but the timer goes all screwy only when not called from the GameControllerScript.
}
Converted your "answer" to a comment. Your comment is not constructive to the question. Please restrain yourself and do not post useless comments as answers in the future.
Heh, yeah, na$$anonymous$$g functions can be a challenging at times :P. $$anonymous$$eeping them short but descriptive.
Answer by aida12_99 · Apr 11, 2016 at 07:25 PM
@Arkaic: Your problem is that you are calling the function from totally another instance of the GameControllerScript! You need to get the same instance of GameControllerScript to have a same timer to count the time correctly.
So, in the Player script try this instead: GameObject.Find("ObjectName").GetComponent<\GameControllerScript\>().FreezeBallsForTime(time);
and instead of "ObjectName" above, put the name of the gameObject that the gameControllerScript belongs to.
Edit: In the code I wrote above just ignore the "backslash ()' before and after GameControllerScript.
Bro, I want to give you the world right now. I have spent at least 10 hours trying to figure this friggen problem out. And that was the problem -_-. Could you please elaborate on how I calling it from another instance?? In the player script I would grab the prefab of the GameController, and there's only ever one instance of that in the game. So how in the world was is calling it and partially working from Another GameController?!! I like to think of myself as pretty much intermediate when it comes to program$$anonymous$$g, but holy cow, i feel like an idiot right now!
Also, you should put this as an answer, so i can mark it as the answer!
So glad that it worked!! :-)) Don't forget to vote ! Unity needs to know exactly what instance of object you are referring to!
Answer by b1gry4n · Apr 11, 2016 at 08:21 AM
if (ballsFreezeTimer > 0){
ballsFreezeTimer -= Time.deltaTime;
if (ballsFreezeTimer <= 0){
Level.UnFreezeBalls();
}
}
if the ballfreezetimer is greater than zero it will count down, but it will never be equal or less than zero to unfreeze them since youve got that bit inside the same check.
if (ballsFreezeTimer > 0){
ballsFreezeTimer -= Time.deltaTime;
}else {
ballsFreezeTimer = 0.0f;
Level.UnFreezeBalls();
}
and dont forget to uncomment this chunk here:
ballsFreezeTimer = 5;//waitTime;
to
ballsFreezeTimer = waitTime;
I also noticed youre doing a check for Level.freeze to cause the timer countdown, but when the balls are initially frozen you are just saying "freeze = true". I dont know your setup, but maybe its tied to this as well...So for consistency sake, either do a check for if(freeze) when the timer is supposed to countdown OR when your balls are frozen do Level.freeze = true;
Hello! Thanks a ton for the response, the outside perspective has helped me look at the problem a bit differently. I am still having the issue, but I'd like to ask some questions, since I may have some bad common practice going on here.
First off, I did change the timer to properly handle the check. Odd thing is, it worked fine when I called the function with keypress inside the script (not even within the Level class). But when called externally it didn't ever reach 0. Which i didn't realize that was the problem, but that could make sense. Wonky thing is, now I've changed it, I still have a similar problem haha. When I call it from keypress (only inside the GameControllerScript) it works perfectly. But when It's called on collision with the player, it goes through everything instantly. It freeze, sets timer and unfreezes in a split second (according to the console). So i'm still really confused on how that works.
Next thing is, with the freeze versus Level.freeze. Correct me if I'm wrong, I am very new to the public class stuff. I actually just started using them specifically to solve the problem i'm having, except its not solving it haha. But as far as I know, when calling a function that defined inside the Level class, you don't need to put the Level class in front of it. Like in my script, I call Level.freeze inside of the Level class, so i just use freeze. Is that wrong to do?
I occasionally will get a random console error talking about Treeviews (something to do with GUI I think) lol. I don't know if this is somehow contributing to issues.
Your answer
Follow this Question
Related Questions
Admob, Can't call a function from HandleUserEarnedReward() for some reason. 0 Answers
How to use a function from another script inside of the Invoke () class? 2 Answers
MouseLock is not working 2 Answers
Boolean From Another Class Not Being Updated 3 Answers
awake and start functions not working 2 Answers