- Home /
Why can't I call function from another script?
Hi people, just wondering if you could have a look at my script. I'm trying to call over a function from another script and it keeps giving me the error that it is not a member of "Object".Here's my scripts
Score Script:
function CheckValueOfScore(){
//Debug.Log("Score equals "+score);
if (score > 299)
{
waterMarkObject = GameObject.FindWithTag("GUITime");
countdownScript = waterMarkObject.GetComponent(Countdown);
currentGUITime = countdownScript.HandOverGUITime();
Debug.Log("ShootScript reads GUITime as "+currentGUITime);
Debug.Log("Countdown reading as "+countdownScript);
yield WaitForSeconds (3);
Application.LoadLevel(3);
Debug.Log("Game over");
}
}
Timer script
var startTime : int;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
var Text;
var customGuiStyle : GUIStyle;
var GUITime :int;
function Awake()
{
startTime = Time.time;
}
function OnGUI()
{
Debug.Log("Real GUITime is "+GUITime);
GUITime = Time.time - startTime;
restSeconds = countDownSeconds - GUITime;
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 60)
{
print ("One Minute Left");
}
if (restSeconds == 0)
{
print ("Time is Over");
Application.LoadLevel(2);
//do stuff here
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
Text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (400, 25, 100, 30), "Time Remaining: " +Text, customGuiStyle);
}
function HandOverGUITime(){
Debug.Log("inside HandOverGUITime()");
return GUITime;
}
Any help would be lovely :D
Why is it "Timer" script if you are trying to get "Countdown" script?
As an aside, please note that OnGUI will be called more than once in a frame, so your 'GUITime' timestamp may not have the meaning that you are intending. Personally, I would avoid updating state in OnGUI and ins$$anonymous$$d only use it for layout and display purposes.
Where is your variable Countdown declared? And your countdownScript variable, can you confirm the type?
@Xroft666: Sorry about that, it is in fact called Countdown, that was an error on my part. Apologies.
@phodges: The final intention of this script is to call the time remaining and then times that number by ten to add to the final score so wouldn't it have to be on GUITime or am I mistaken?
@$$anonymous$$emige: Bugger, me and my lecturer hadn't noticed that the variable countdown isn't declared. Also, could you tell me how to check the type of a variable? Won't know if not declaring countdown is the workaround but if so will let you all know tomorrow when I have access to me work.
In the meantime, any more helpful hints/ideas? =]
You could use an Update function to handle tracking state and changing level, reserving OnGUI for display. Since you only seem to care about the time remaining, you could add an explicit variable (i.e. var timeRemaining : float), which would be initialised at the beginning of the round and decremented using Time.deltaTime.
Answer by Sonaten · Oct 25, 2012 at 09:27 PM
To address the question, even though it seems that you are getting a lot of good hints on 'other' topics. :)
To call a function in ScriptA from another ScriptB you will have to let ScriptA know about ScriptB. Like so in ScriptA:
public var scriptKeeper : SctiptB;
function Start ()
{
scriptKeeper = GameObject.Find("NameOfObjectWithScriptB").transform.GetComponent<"ScriptB">();
}
You would then be able to use
scriptKeeper.wantedFunctionName();
from within ScriptA, provided that the function is public.
I admit that I did not take the time to look through your scripts closely, thus from the question title this seemed to fit as a posible solution.
I hope that this can help you out a bit.
That's incorrect syntax, and GetComponent works on game objects, not transforms.
script$$anonymous$$eeper = GameObject.Find("NameOfObjectWithScriptB").GetComponent(ScriptB);