Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/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_Compiler Errors - Fatal编程技术网

C# 我不明白为什么unity会在这里出现编译错误

C# 我不明白为什么unity会在这里出现编译错误,c#,unity3d,compiler-errors,C#,Unity3d,Compiler Errors,我试图更改TMPro文本框的文本,但是unity在20,79报告我需要一个}。我把它放在括号里,会弹出11个错误。调试pls?我自己知道的不多,所以我自己做不了。请帮忙 using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class tetxmeshpro : MonoBehaviour { int previoushealth = 100; p

我试图更改TMPro文本框的文本,但是unity在20,79报告我需要一个}。我把它放在括号里,会弹出11个错误。调试pls?我自己知道的不多,所以我自己做不了。请帮忙

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class tetxmeshpro : MonoBehaviour
{ int previoushealth = 100;
  private TextMeshProUGUI textMesH;

    // Start is called before the first frame update
    void Start()
    {

        var Player; GameObject;
        textMesH = GetComponent<TextMeshProGUI> ();
    }

    // Update is called once per frame
    void Update()
    {
        if ((Player.GetComponent(playerScript).Health)) not == previoushealth;)
        {
            previoushealth = Player.GetComponent(playerScript).Health;
            textMesH = previoushealth;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用TMPro;
公共类tetxmeshpro:MonoBehavior
{int previoushealth=100;
私有textMesH prougui textMesH;
//在第一帧更新之前调用Start
void Start()
{
var玩家;游戏对象;
textMesH=GetComponent();
}
//每帧调用一次更新
无效更新()
{
if((Player.GetComponent(playerScript.Health))not==previoushealth;)
{
previoushealth=Player.GetComponent(playerScript.Health);
textMesH=以前的健康状况;
}
}
}
  • 到底是什么

    var Player; GameObject;
    
    应该做什么?;)

    看来你相当刻薄

    GameObject Player;
    
    无论哪种方式,
    Player
    都不是一个字段,因此稍后在
    Update
    中您也无法访问它。你可能更想要一个

    [SerializeField] private GameObject Player; 
    
    在类级别上,以便可以在其中引用对象。或者最好马上给它正确的类型:

    [SerializeField] private playerscript player;
    
    这样以后就不需要
    GetComponent

  • 这是什么

    if ((Player.GetComponent(playerScript).Health)) not == previoushealth;)
    
    这是
    c
    而不是一些Shell/Dos脚本->使用
    =和非
    非==
    。如果
    条件没有以结束,也可以使用
    而且有一个
    太多了。而且
    GetComponent
    要么是泛型的,要么是
    GetComponent()
    ,要么你传入一个类型,但是你必须像
    ((T)GetComponent(typeof(T)))那样强制转换它

    如果是某件事,它应该是简单的

    if (Player.GetComponent<playerScript>().Health != previoushealth)
    
    if(Player.GetComponent().Health!=previoushealth)
    
  • 你的班级应该看起来像

    public class tetxmeshpro : MonoBehaviour
    { 
        int previoushealth = 100;
    
        // Link these via the Inspector using drag&drop 
        [SerializeField] private TextMeshProUGUI _textMesH;
        [SerializeField] private playerscript _player;
    
        // Start is called before the first frame update
        private void Start()
        {
            if(!_textMesH) _textMesH = GetComponent<TextMeshProGUI>();
        }
    
        private void Update()
        {
            if (_player.Health != previoushealth)
            {
                previoushealth = _player.Health;
                textMesH = previoushealth;
            }
        }
    }
    
    公共类tetxmeshpro:MonoBehavior
    { 
    int-previoushealth=100;
    //使用拖放功能通过Inspector链接这些
    [SerializeField]私有textMesH Prougui\u textMesH;
    [SerializeField]私人玩家脚本播放器;
    //在第一帧更新之前调用Start
    私有void Start()
    {
    如果(!\u textMesH)\u textMesH=GetComponent();
    }
    私有void更新()
    {
    如果(_player.Health!=先前的健康)
    {
    previoushealth=\u player.Health;
    textMesH=以前的健康状况;
    }
    }
    }
    

    但是,请注意,命名类
    textmeshpro
    a)可能会与现有的
    textmeshpro
    产生混淆,并且根本不能反映该类的用途。我建议你做一些类似于健康显示之类的事情
    或任何类似的有意义的事情。

    谢谢你报道了所有事情,但你说的话有一半欺骗了我。