从另一个脚本通过GetComponent访问变量在另一个游戏物体

从另一个脚本通过GetComponent访问变量在另一个游戏物体

本文介绍了如何从另一个脚本通过GetComponent访问变量在另一个游戏物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜索周围,我只是不能得到这个工作。我想我只是不知道正确的语法,或者只是没有十分把握的情况下。

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.

我有持有公众诠释一个BombDrop脚本。我得到这个与公共静态工作,但有人说,这是一个非常糟糕的编程习惯,我应该学会封装。下面是我写的:

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脚本:

 <!-- language: c# -->

 public class BombDrop : MonoBehaviour {

 public GameObject BombPrefab;

 //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.position;
     pos.x = Mathf.Round(pos.x);
     pos.y = Mathf.Round(pos.y);
     Instantiate(BombPrefab, pos, Quaternion.identity);

 }
}



所以,我想弹脚本是附着在prefabgameobject Bombprefab访问BombDrop的maxBombs整​​数,使得当炸弹被破坏它增加了+之一在BombDrop maxBombs

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 GameObject explosion;

 BoxCollider2D collider;

 private BombDrop BombDropScript;




 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 = false;
 }

 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>();



但是,这并不工作。也许我只是不明白这里的语法。难道是想说otherGameObject?因为这并不做任何事情。我仍然得到错误:对象未设置做一个对象的实例在我BombDropScript.maxBombs倒在爆炸()

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'll expand a bit on your question since I already answered it.

你的代码正在做的是说你看我的游戏对象为 BombDropScript 中,大部分的时间脚本将不会被连接到相同的游戏对象。

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.

同时使用 maxBombs 的setter和getter。

Also use a setter and getter for maxBombs.

public class BombDrop : MonoBehaviour
{
    public void setMaxBombs(int amount)
    {
        maxBombs += amount;
    }

    public int getMaxBoms()
    {
        return maxBombs;
    }
}

这篇关于如何从另一个脚本通过GetComponent访问变量在另一个游戏物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:35