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;
    }
}
Celestial Riot
  • 15
  • 1
  • 1
  • 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. – Programmer Aug 05 '17 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? – Celestial Riot Aug 05 '17 at 14:26
  • You have a script called `triggered`? – Programmer Aug 05 '17 at 14:28
  • @Programmer Yes, its under the game object added to the public var – Celestial Riot Aug 05 '17 at 14:32

1 Answers1

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328