Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity中显示收集的硬币数量而不溢出(访问另一个脚本)?_C#_Unity3d_Stack Overflow - Fatal编程技术网

C# 如何在Unity中显示收集的硬币数量而不溢出(访问另一个脚本)?

C# 如何在Unity中显示收集的硬币数量而不溢出(访问另一个脚本)?,c#,unity3d,stack-overflow,C#,Unity3d,Stack Overflow,我有两个脚本PlayerMove.cs和Spawner.cs。 在PlayerMove.cs中,我需要一个将文本更改为硬币数量的函数 void CountCoin() { cointext.text = ": "+ coincount; CountCoin(); } 在Spawner.cs中,我会生成硬币,并且我希望每次生成硬币时增加硬币数量(这是PlayerMove.cs的公共变量) IEnumerator StartSpawningCoins () {

我有两个脚本PlayerMove.csSpawner.cs。 在PlayerMove.cs中,我需要一个将文本更改为硬币数量的函数

void CountCoin()
{
    cointext.text = ": "+ coincount;
    CountCoin();
}
在Spawner.cs中,我会生成硬币,并且我希望每次生成硬币时增加硬币数量(这是PlayerMove.cs的公共变量)

IEnumerator StartSpawningCoins ()
    {
        coin.GetComponent<Renderer>().enabled=true;

        yield return new WaitForSeconds(Random.Range(6f, 16f));

        GameObject k = Instantiate(coin);

        // my problem starts here

        GameObject Player = GameObject.Find("PlayerMove");
        PlayerMove playermove = Player.GetComponent<PlayerMove>();
        playermove.coincount++;

        //till here

        float x = Random.Range(min_X,max_X);

        k.transform.position = new Vector2(x, transform.position.y);

        StartCoroutine(StartSpawningCoins());
    }
IEnumerator启动典当币()
{
coin.GetComponent().enabled=true;
收益率返回新的WaitForSeconds(随机范围(6f,16f));
游戏对象k=实例化(硬币);
//我的问题从这里开始
GameObject Player=GameObject.Find(“PlayerMove”);
PlayerMove PlayerMove=Player.GetComponent();
playermove.coincount++;
//直到这里
浮动x=随机范围(最小值x,最大值x);
k、 transform.position=新矢量2(x,transform.position.y);
start例程(StartSpawningCoins());
}
当我使用这些时,我得到堆栈溢出错误。我怎样才能修复它呢?

问题在于

void CountCoin()
{
    cointext.text = ": "+ coincount;
    CountCoin();
}
您可以调用
CountCoin()
本身


如果您需要重复更新,您可以使用

或者直接

为了每一秒都能打电话

或者干脆完全删除嵌套调用,然后添加一个,使其每帧都被调用一次

void Update()
{
    CountCoin();
}
这是我更愿意做的:使
硬币计数
a并根据setter添加相应的行为:

private int _coincount;
public int coincount 
{
    get { return _coincount; }
    set 
    { 
        _coincount = value;
        cointext.text = ": "+ value;
    }
}  
因此,每次更改属性时,代码都会自动执行


顺便说一句,Find非常昂贵,应该避免使用。我会把你的密码改成

private PlayerMove playermove;

IEnumerator StartSpawningCoins ()
{
    // as long as you yield inside this is ok
    while(true)
    {
        // only do find if needed
        if(!player) player = GameObject.Find("PlayerMove");

        // this should probably actually be done in the prefab not via code
        coin.GetComponent<Renderer>().enabled = true;

        yield return new WaitForSeconds(Random.Range(6f, 16f));

        GameObject k = Instantiate(coin);
        playermove.coincount++;

        float x = Random.Range(min_X,max_X);

        k.transform.position = new Vector2(x, transform.position.y);
    }
}
private PlayerMove PlayerMove;
IEnumerator开始典当硬币()
{
//只要你在这里面让步就可以了
while(true)
{
//只有在需要的时候才能找到
如果(!player)player=GameObject.Find(“PlayerMove”);
//这实际上应该在预置中完成,而不是通过代码
coin.GetComponent().enabled=true;
收益率返回新的WaitForSeconds(随机范围(6f,16f));
游戏对象k=实例化(硬币);
playermove.coincount++;
浮动x=随机范围(最小值x,最大值x);
k、 transform.position=新矢量2(x,transform.position.y);
}
}

PlayerMove脚本附加到的游戏对象的名称是什么?…它是Playervoid CountCoin(){cointext.text=“:”+coincount;CountCoin();}删除此函数中对CountCoin()的函数调用并选中。CountCoin函数在这里被无限地调用
CountCoin()
调用自身,没有退出条件,因此最终进入无限循环。
private int _coincount;
public int coincount 
{
    get { return _coincount; }
    set 
    { 
        _coincount = value;
        cointext.text = ": "+ value;
    }
}  
private PlayerMove playermove;

IEnumerator StartSpawningCoins ()
{
    // as long as you yield inside this is ok
    while(true)
    {
        // only do find if needed
        if(!player) player = GameObject.Find("PlayerMove");

        // this should probably actually be done in the prefab not via code
        coin.GetComponent<Renderer>().enabled = true;

        yield return new WaitForSeconds(Random.Range(6f, 16f));

        GameObject k = Instantiate(coin);
        playermove.coincount++;

        float x = Random.Range(min_X,max_X);

        k.transform.position = new Vector2(x, transform.position.y);
    }
}