如何转换c#中的other.tag? [英] How to convert other.tag in c#?

查看:115
本文介绍了如何转换c#中的other.tag?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Debug.Log(  OnTriggerEnter()被称为 ); 
if (other.tag == 硬币){
Debug.Log( 其他对象是硬币);
得分+ = 10 ;
scoreText = 得分: +得分;
Debug.Log( 分数现在是 +得分);
Destroy(other.gameObject);







每次放if时都会出错(other.tag = =硬币)在c#



代码块添加 - OriginalGriff [/ edit]

解决方案

< blockquote>原因可能是Tag属性通常是对象对象不是字符串,所以你无法将其与一个进行比较。



将Tag属性转换为您期望的类,然后使用它:

  string  s = other.tag  as   string ; 
如果(s!= null & s == Coin
...


Debug.Log("OnTriggerEnter() was called");
if (other.tag  == "Coin") {
    Debug.Log("Other object is a coin");
    score += 10;
    scoreText = "Score: " + score;
    Debug.Log("Score is now " + score);
    Destroy(other.gameObject);




There is always an error every time I put if(other.tag == "Coin") in c#

[edit]Code block added - OriginalGriff[/edit]

解决方案

The reason is probably that a Tag property is normally an object and an object is not a string, so you can't compare it with one.

Cast the Tag property to the class you are expecting, and use it then:

string s = other.tag as string;
if (s != null & s == "Coin")
   ...


这篇关于如何转换c#中的other.tag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆