1

I have a code block that starts a timer, enables a script, instantiates, then disables that script (this makes my game mechanic work out). The only issue I'm facing now is my Javascript code isn't recognising a component (triggered) error reads script as undeclared from the public variable (which has been added, and which holds the script).

I followed the example that Unity had, and that didn't seem to work out. Any help is appreciated. Here is my code:

#pragma strict

var enemy : Transform;
private var timer : float;
public var mother : GameObject;

function Awake() {
    timer = Time.time + 14;
    script = mother.GetComponent(triggered);
    script.enabled = script.enabled;
}

function Update() {
    if (timer < Time.time) {
        Instantiate(enemy, transform.position, transform.rotation);
        timer = Time.time + Random.Range(55, 60);
        script.enabled = !script.enabled;
    }
}
4
  • 1.You did not describe your problem. Do you get any error? What's really happening with this code? 2.script is not declared. Why are you trying to access/write to a variable that is not even declared? 3.You should be using C# instead of Unityscript/Javascript. You will need that if you want to create serious games in the future. Commented Aug 5, 2017 at 13:52
  • @Programmer This is my first shot at coding a game, so I'm not very experienced with which language to use ;). Yes the error was for it being undeclared, but isn't script declared as the component of mother? How could this be fixed? Would adding private var script = mother.GetComponent(triggered); outside and above the Awake function fix it? Commented Aug 5, 2017 at 14:26
  • You have a script called triggered? Commented Aug 5, 2017 at 14:28
  • @Programmer Yes, its under the game object added to the public var Commented Aug 5, 2017 at 14:32

1 Answer 1

0

The script variable is not declared.

#pragma strict

var enemy : Transform;
private var timer : float;
public var mother : GameObject;

//DECLARE IT
private var script : triggered;

function Awake() {
    timer = Time.time + 14;
    script = mother.GetComponent(triggered);
    script.enabled = script.enabled;
}

function Update() {
    if (timer < Time.time) {
        Instantiate(enemy, transform.position, transform.rotation);
        timer = Time.time + Random.Range(55, 60);
        script.enabled = !script.enabled;
    }
}

Note:

Please learn C# and start using it in Unity. You will save yourself your time. Also, when creating scripts make sure to capitalize them. For example, triggered should be Triggered.

Sign up to request clarification or add additional context in comments.

1 Comment

Huge noob mistake, damn it. I'll take your word and be sure to learn my Cs if I decide to continue using Unity. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.