GetComponent无法识别Unity JavaScript代码中的组件 [英] GetComponent not recognising component in Unity JavaScript code
问题描述
我有一个代码块,用于启动计时器,启用脚本,实例化然后禁用该脚本(这使我的游戏机制得以解决).我现在面临的唯一问题是我的Javascript代码无法识别(触发)的组件 error从公共变量(已添加并保存脚本)中将脚本读为未声明.
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).
我遵循了Unity的示例,但似乎没有解决.任何帮助表示赞赏.这是我的代码:
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;
}
}
推荐答案
script
变量未声明.
#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;
}
}
注意:
请学习 C#,并开始在Unity中使用它.您将节省您的时间.另外,在创建脚本时,请确保将其大写.例如,triggered
应该为Triggered
.
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
.
这篇关于GetComponent无法识别Unity JavaScript代码中的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!