- Home /
Question by
Alanj2007 · Oct 05, 2013 at 05:35 AM ·
guivariablegetcomponent
Any problems with this script?
I attached script B to generators (When you press "E", value of "CountGenerator" in Script A will increase)
//Script B
var message : boolean = false;
var GeneratorGUI : GameObject;
var Generator1 : GameObject;
var useGui : boolean = true;
function OnTriggerStay(other : Collider)
{
if(Input.GetButton("e"))
{
GeneratorGUI.GetComponent(GeneratorCountNumbers).addCount(1);
disableGui();
}
}
function disableGui ()
{
Generator1.GetComponent(ActiveGenerator).useGui = false;
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}
function OnGUI()
{
if(message)
{
if(useGui)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to turn on the generator");
}
}
}
This is script A attached to Emptygameobject and called "GeneratorGUI"
//Script A
public static var CountGenerator : int = 0;
var Onemore : GUIText;
var Activated : GUIText;
var Gate : GameObject;
var GateOpenSound : AudioSource;
function addCount(int value)
{
CountGenerator += value;
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
}
if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
}
I got an error
Assets/GeneratorCountNumbers.js(9,23): BCE0043: Unexpected token: value.
Comment
Best Answer
Answer by TrickyHandz · Oct 05, 2013 at 05:49 AM
The syntax of your AddCount function is written as if it were C#, when it should be in JS. Instead of this:
function AddCount(int value)
it should read
function AddCount(value : int)
Thanks! It really access the script. But there is one problem. If statements don't actually working perfectly
I said if int = 1
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
}
It will enabled the GUI. This is working perfectly. It's actually enabled it but int = 2 does not work
if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
}
It's still access int = 2 although int just equal = 1