unity3d 调用其他脚本一个脚本如何调用另一个脚本的东西

I've searched around and I just can't get this to work. I think I just don't know the proper syntax, or just doesn't quite grasp the context.
I have a BombDrop script that holds a public int. I got this to work with public static, but Someone said that that is a really bad programming habit and that I should learn encapsulation. Here is what I wrote:
BombDrop script:
&!-- language: c# --&
public class BombDrop : MonoBehaviour {
public GameObject BombP
//Bombs that the player can drop
public int maxBombs = 1;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
if(maxBombs & 0){
DropBomb();
//telling in console current bombs
Debug.Log("maxBombs = " + maxBombs);
void DropBomb(){
// remove one bomb from the current maxBombs
maxBombs -= 1;
// spawn bomb prefab
Vector2 pos = transform.
pos.x = Mathf.Round(pos.x);
pos.y = Mathf.Round(pos.y);
Instantiate(BombPrefab, pos, Quaternion.identity);
So I want the Bomb script that's attached to the prefabgameobject Bombprefab to access the maxBombs integer in BombDrop, so that when the bomb is destroyed it adds + one to maxBombs in BombDrop.
And this is the Bomb script that needs the reference.
public class Bomb : MonoBehaviour {
// Time after which the bomb explodes
float time = 3.0f;
// Explosion Prefab
public GameO
BoxCollider2D
private BombDrop BombDropS
void Awake (){
BombDropScript = GetComponent&BombDrop& ();
void Start () {
collider = gameObject.GetComponent&BoxCollider2D& ();
// Call the Explode function after a few seconds
Invoke("Explode", time);
void OnTriggerExit2D(Collider2D other){
collider.isTrigger =
void Explode() {
// Remove Bomb from game
Destroy(gameObject);
// When bomb is destroyed add 1 to the max
// number of bombs you can drop simultaneously .
BombDropScript.maxBombs += 1;
// Spawn Explosion
Instantiate(explosion,
transform.position,
Quaternion.identity);
In the documentation it says that it should be something like
BombDropScript = otherGameObject.GetComponent&BombDrop&();
But that doesn't work. Maybe I just don't understand the syntax here. Is it suppose to say otherGameObject? Cause that doesn't do anything. I still get the error : "Object reference not set do an instance of an object" on my BombDropScript.maxBombs down in the explode()
解决方案 You need to find the GameObject that contains the script Component that you plan to get a reference to. Make sure the GameObject is already in the scene, or Find will return null.
GameObject g = GameObject.Find("GameObject Name");
Then you can grab the script:
BombDrop bScript = g.GetComponent&BombDrop&();
Then you can access the variables and functions of the Script.
bScript.foo();
I just realized that I answered a very similar question the other day, check here:
I'll expand a bit on your question since I already answered it.
What your code is doing is saying "Look within my GameObject for a BombDropScript, most of the time the script won't be attached to the same GameObject.
Also use a setter and getter for maxBombs.
public class BombDrop : MonoBehaviour
public void setMaxBombs(int amount)
maxBombs +=
public int getMaxBoms()
return maxB
本文地址: &
我已搜索周围,我只是不能得到这个工作。我想我只是不知道正确的语法,或者只是没有十分把握的情况下。
我有持有公众诠释一个BombDrop脚本。我得到这个与公共静态工作,但有人说,这是一个非常糟糕的编程习惯,我应该学会封装。下面是我写的:
的 BombDrop脚本:的
- 语言:C# - >
公共类BombDrop:MonoBehaviour { 公共游戏物体BombP
//炸弹,玩家可以删除公众诠释maxBombs = 1;
//更新是每帧无效更新调用一次(){ 如果(Input.GetKeyDown(KeyCode.Space)){ 如果(maxBombs大于0){ DropBomb();
//在控制台告诉当前炸弹的debug.log(“maxBombs =”+ maxBombs); } }
无效DropBomb(){
//从删除一个炸弹当前maxBombs
//产卵炸弹预制 Vector2 POS = transform.
pos.x = Mathf.Round(pos.x);
pos.y = Mathf.Round(pos.y); 实例化(BombPrefab,POS,Quaternion.identity);
所以,我想弹脚本是附着在prefabgameobject Bombprefab访问BombDrop的maxBombs整数,使得当炸弹被破坏它增加了+之一在BombDrop maxBombs
这是炸弹脚本。需要参照
公共类炸弹:MonoBehaviour { //时间之后,炸弹爆炸时差= 3.0F;
//爆炸预制公共游戏物体爆炸;
BoxCollider2D对撞机;
私人BombDrop BombDropS
无效唤醒(){
BombDropScript = GetComponent< BombDrop> (); }
无效的start(){ 撞机= gameObject.GetComponent< BoxCollider2D> ();
//调用几秒钟调用后爆炸功能(“爆炸”,时间);
无效OnTriggerExit2D(Collider2D等){
collider.isTrigger = FALSE; }
无效爆炸(){ //从游戏卸下炸弹摧毁(游戏对象);
//当炸弹被销毁加1到最大 //炸弹的数量可以同时下降。
BombDropScript.maxBombs + = 1;
//菌种防爆实例化(爆炸, transform.position, Quaternion.identity);
在文档它说,它应该是类似
BombDropScript = otherGameObject.GetComponent< BombDrop>();
但是,这并不工作。也许我只是不明白这里的语法。难道是想说otherGameObject?因为这并不做任何事情。我仍然得到错误:“对象未设置做一个对象的实例”在我BombDropScript.maxBombs倒在爆炸()
解决方案 您需要找到包含您计划去一个参考脚本组件的游戏对象。确保游戏对象已经在现场,或查找将返回空。
游戏物体G = GameObject.Find(“游戏对象名称”);
然后你可以抓住这个脚本:
BombDrop bScript = g.GetComponent< BombDrop>();
然后就可以访问该脚本的变量和函数。
bScript.foo();
我才意识到,我回答了一个非常类似的问题有一天,点击这里:
我会扩大你的问题有点,因为我已经回答了。
你的代码正在做的是说“你看我的游戏对象为 BombDropScript 中,大部分的时间脚本将不会被连接到相同的游戏对象。
同时使用 maxBombs 的setter和getter。
公共类BombDrop:MonoBehaviour
{公共无效setMaxBombs(INT金额) { maxBombs + =金额;
公众诠释getMaxBoms() {返回maxB } }
本文地址: &
扫一扫关注官方微信游戏开发之unity解决脚本相互调用的几种方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
游戏开发之unity解决脚本相互调用的几种方法
上传于|0|0|文档简介
&&游戏开发之unity解决脚本相互调用的几种方法介绍。
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
你可能喜欢13:13 提问
unity3D 如何在一个script里控制另一个物体上的script的enable;
求大神帮助指导!!!!unity3D 如何在一个script里控制另一个物体上的script的enable;
其他相似问题1044人阅读
Unity3d热更新 -- SLua(22)
项目中使用 SLua 是通过一个中间件,比如 LuaBehaviour.cs 。在 Prefab 上面添加 LuaBehaviour 组件,然后在里面进行 DoFile 操作 执行 Lua 的Chunk 。
今天遇到问题,如何从GameObjectA 上挂的 LuaA& 调用 GameObjectB 上挂的 LuaB 中的函数?
想不通就去问同事大神了。一番讲解后,个人理解如下:
LuaManager.Instance.DoFile(luaScriptPath)
这个接口,返回的Object ,其实就是 返回一个 LuaTable !!
我们只要获取到这个 LuaTable, 就可以调用这个 LuaTable 中的函数了!!
把 DoFile 返回的 LuaTable 保存为成员变量m_LuaTable,供 Lua 中使用。
于是整理流程如下:
gameObjectB:SetActive(true)
local luaBehaviour=gameObjectB.transform:GetComponent(LuaBehaviour)
local luaTable=luaBehaviour.m_LuaTable --获取到LuaTable
luaTable:Show(100)
--调用函数
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:847696次
积分:11899
积分:11899
排名:第1026名
原创:316篇
转载:108篇
评论:212条
文章:32篇
阅读:91674
(5)(1)(3)(3)(3)(5)(6)(4)(7)(1)(18)(18)(17)(13)(9)(24)(25)(2)(7)(5)(9)(17)(11)(7)(25)(8)(9)(1)(4)(12)(6)(2)(3)(16)(21)(10)(14)(1)(7)(25)(11)(6)(13)(4)(8)(7)(2)

我要回帖

更多关于 unity 调用脚本 的文章

 

随机推荐