问题描述
我四处搜索,但我无法让它发挥作用.我想我只是不知道正确的语法,或者只是不太了解上下文.
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);
}
}
所以我希望附加到预制游戏对象 Bombprefab 的 Bomb 脚本访问 BombDrop 中的 maxBombs 整数,这样当炸弹被摧毁时,它会将 BombDrop 中的 maxBombs 加 1.
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.
这是需要参考的 Bomb 脚本.
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 中,我的 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()
推荐答案
您需要找到包含您计划引用的脚本组件的 GameObject.确保游戏对象已经在场景中,否则 Find
将返回 null
.
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:Don't know how to get enemy's health
因为我已经回答了你的问题,所以我会稍微扩展一下.
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 getMaxBombs()
{
return maxBombs;
}
}
这篇关于如何通过GetComponent从另一个游戏对象中的另一个脚本访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!