从脚本到脚本传输参数,如何?
Transmitting parameters from script to script, how?
我知道这个问题有几个答案,但它们对我不起作用,或者我只是太蠢了。对不起。
这是我的脚本:
menu_options.js - http://pastebin.com/NFXD3ZgP
Transmitter.js - http://pastebin.com/kCmGTgbH
我已将 Transmitter.js 附加到特定对象。它叫做“_Transmitter”。
如果我 运行 我的脚本会出现以下错误:
Assets/Script/menu_options.js(7,40): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found.
为什么?这是什么意思?
我在教程和答案中理解错了什么?
我也试过这个:
trans = Transmitter.gameObject.GetComponent(Transmitter);
还有这个:
trans = Transmitter.GetComponent(Transmitter);
要将参数传递给另一个脚本,您必须首先引用该脚本。
脚本A
function Start()
{
var reference : GameObject = GameObject.Find("Name Of GameObject").GetComponent("ScriptB");
reference.setValue(50);
}
所以我们Find()
拥有脚本的游戏对象,我们首先需要拥有它的游戏对象的名称。然后我们使用 GetComponent()
来获取脚本的引用。然后我们进行函数调用以设置参数。
脚本B
private var myValue : int;
function setValue(var amount : int)
{
myValue = amount;
}
PlayerPrefs.SetInt("variablenname", 1);
和
var v1 = PlayerPrefs.GetInt("variablenname");
效果很好!
我知道这个问题有几个答案,但它们对我不起作用,或者我只是太蠢了。对不起。
这是我的脚本:
menu_options.js - http://pastebin.com/NFXD3ZgP
Transmitter.js - http://pastebin.com/kCmGTgbH
我已将 Transmitter.js 附加到特定对象。它叫做“_Transmitter”。
如果我 运行 我的脚本会出现以下错误:
Assets/Script/menu_options.js(7,40): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found.
为什么?这是什么意思?
我在教程和答案中理解错了什么?
我也试过这个:
trans = Transmitter.gameObject.GetComponent(Transmitter);
还有这个:
trans = Transmitter.GetComponent(Transmitter);
要将参数传递给另一个脚本,您必须首先引用该脚本。
脚本A
function Start()
{
var reference : GameObject = GameObject.Find("Name Of GameObject").GetComponent("ScriptB");
reference.setValue(50);
}
所以我们Find()
拥有脚本的游戏对象,我们首先需要拥有它的游戏对象的名称。然后我们使用 GetComponent()
来获取脚本的引用。然后我们进行函数调用以设置参数。
脚本B
private var myValue : int;
function setValue(var amount : int)
{
myValue = amount;
}
PlayerPrefs.SetInt("variablenname", 1);
和
var v1 = PlayerPrefs.GetInt("variablenname");
效果很好!