- Home /
script connection
I have two JavaScript files one for a main building in my rts and one for a control point. I have managed to get them to some what communicate but the GUI in my control point script gets messed up, how can I fix this? I am still relatively new to scripting so be gentle. Thanks in advance
main building script-
//MainBuildScript
var curMoney : float;
var mob1 : GameObject;
var theLocation : Transform;
var check : int = 1;
var conCheck : int;
var loop : boolean = true;
var go = GameObject.Find("controlPoint");
function Update ()
{
curMoney = go.GetComponent(controlPointScript2).curRes;
conCheck = go.GetComponent(controlPointScript2).checkMe;
if(check > 1)
{
check = 1;
}
}
function OnGUI()
{
GUI.Label(Rect (10, 10, 100, 20), "Main");
if (GUI.Button(Rect(10,30,80,30), "Worker"))
{
if(curMoney >= 5.0)
{
loop = true;
mob1.SetActiveRecursively(true);
Instantiate (mob1 , theLocation.position, transform.rotation);
mob1.SetActiveRecursively(false);
subtractFive();
}
}
}
function subtractFive ()
{
if(loop)
{
//Subtract Money
check = 2;
loop = false;
}
}
Control Point Script -
var curRes : float = 0;
var timer : float;
var go2 = GameObject.Find("testMainBuilding");
var checkMe : int;
function Update ()
{
checkMe = go2.GetComponent(mainBuildScript).check;
if(curRes > 1000)
{
curRes = 1000;
}
if(checkMe >= 2)
{
curRes = curRes - 5.0;
}
if(curRes <= 0)
{
curRes = 0;
}
}
function OnCollisionStay (thing : Collision)
{
timer += Time.deltaTime;
if(thing.gameObject.tag == "control")
{
if(timer >= 5)
{
curRes = curRes + 5.0;
timer = 0;
}
}
}
function OnGUI()
{
GUI.Label (Rect (110, 10, 100, 20), "Resources: " + curRes);
}
Answer by aldonaletto · Apr 07, 2012 at 04:41 AM
You can't declare a variable and assign GameObject.Find(...) to it in the same line (as well as many other Unity functions or properties). Furthermore, the code outside any function is executed at Awake, when the object you're trying to find may not exist yet.
To avoid these problems, you should just first declare the variable, then assign Find at Start():
... var go: GameObject; // declare the variable first...
function Start(){ // and find the object at Start(): go = GameObject.Find("controlPoint"); } The same applies to the second script.
I did what you said but I still get the GUI messing up and for some reason, the cur$$anonymous$$oney and curRes variables are still not equal. Any further help would be greatly appreciated!
the cur$$anonymous$$oney is not affected by the -5 for some reason?
Yep, and that would be because the Control Point Script can't find the check variable in your mainBuildScript because it can't find the object it's assigned to.
Did you do what @aldonaletto told you to do in the Control Point Script as well?
var go2 = GameObject.Find("test$$anonymous$$ainBuilding");
Hope that helps, $$anonymous$$lep :P
@$$anonymous$$leptomaniac is right: both scripts should be fixed, as I told at the end of my answer. But the whole thing is too complex: flags that set/clear other flags, that are used in another script to modify a value that's read back by the first script in Update, and with OnGUI and Collision events interlaced... I think you should make a flowchart, then rewrite the whole thing! And pay attention to the error messages: you should have at least 3 of them with the original code.
ok thanks everyone for being so helpful! Ill re-write the script and make it less complex.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
How do I correct the following error on the Lerpz Tutortial GUI 1 Answer
Tracking Down GUI Errors 0 Answers
"NullReferenceException" while trying to draw a texture (C#) 1 Answer
I keep getting errors please help this script is to make gui appear if trigger is on 1 Answer