我今天在Unity3D中创建了一个简单的场景,玩家可以通过碰撞到墙壁来选择颜色,这会将玩家的标签更改为“RedPlayer”,“BluePlayer”等;并在调试日志上打印出“他是个红色男孩!”,或者玩家选择的任何内容。然而,在我添加最后一个墙(WhitePlayer)之前,一切都工作正常,并且每个墙都将玩家的标签更改为WhitePlayer,但它仍然会打印出与之碰撞的墙的颜色。
using UnityEngine;
using System.Collections;
public class ColourChoice : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "RedCollision")
Debug.Log("He's a Red Boy!");
gameObject.tag = "RedPlayer";
if (other.gameObject.tag == "YellowCollision")
Debug.Log("He's a Yellow Boy!");
gameObject.tag = "YellowPlayer";
if (other.gameObject.tag == "BlueCollision")
Debug.Log("He's a Blue Boy!");
gameObject.tag = "BluePlayer";
if (other.gameObject.tag == "GreyCollision")
Debug.Log("He's a Grey Boy!");
gameObject.tag = "GreyPlayer";
if (other.gameObject.tag == "GreenCollision")
Debug.Log("He's a Green Boy!");
gameObject.tag = "GreenPlayer";
if (other.gameObject.tag == "MaroneCollision")
Debug.Log("He's a Marone Boy!");
gameObject.tag = "MaronePlayer";
if (other.gameObject.tag == "PurpleCollision")
Debug.Log("He's a Purple Boy!");
gameObject.tag = "PurplePlayer";
if (other.gameObject.tag == "OrangeCollision")
Debug.Log("He's a Orange Boy!");
gameObject.tag = "OrangePlayer";
if (other.gameObject.tag == "WhiteCollision")
Debug.Log("He's a White Boy!");
gameObject.tag = "WhitePlayer";
}
}
我已将代码包含在此处,希望有人能够弄明白。如果我能解释如何解决这个问题,那就太棒了。
谢谢, 马特
答案 0 :(得分:2)
在添加if
代码检查之前,您的代码已经很糟糕了。这是新程序员的典型错误。
在每个Debug.Log
语句后没有打开和关闭括号,if
只有在true
语句为if
时才会运行。 Debug.Log下的其他代码将执行,无论其上面的if语句是否为真。
如果您想要执行多行代码,则必须将true
语句代码放在花括号{}
中 if (other.gameObject.tag == "RedCollision")
Debug.Log("He's a Red Boy!");
gameObject.tag = "RedPlayer";
的内容中。
所以
if (other.gameObject.tag == "RedCollision"){
Debug.Log("He's a Red Boy!");
gameObject.tag = "RedPlayer";
}
应该是
if (other.gameObject.CompareTag("RedCollision"))
对代码中的每个if语句执行此操作。
Tom改进了您的代码,使用tags
来比较function printschedule(WhichSheet) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var objToSheetName = {"1":"Schedule 1","2":"Schedule 2","3":"Schedule 3"};
var whichSheetToPDF = objToSheetName[WhichSheet];
var ui = SpreadsheetApp.getUi();
var prompt = ui.prompt('Send to which Email Address?');
var email = prompt.getResponseText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(ss.getSheetByName(whichSheetToPDF));
var sheet = ss.getActiveSheet();
var subject = "Schedules";
var body = "";
var url =
"https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=letter' // paper size legal / letter / A4
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=true' // repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId()).getBlob().setName('Namexyz');
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
attachments:response,
});
};
。
答案 1 :(得分:1)
问题是你的if
没有大括号(OnTriggerEnter
)。
在gameObject.tag = "WhitePlayer";
函数中运行的最后一行代码是WhiteCollision
答案 2 :(得分:0)
尽管非常干净,但你不能在“简短陈述”中写出两行代码if
请注意,您没有{}
附上两行 then
?你不能那样做。简短或简短陈述仅适用于一行和一行。大多数时候看上去都很好,但如果你收到错误并且在调试这些错误时没有将它包含在“正确的”if语句中,那么它就会非常浪费。
要回答您的问题,请更改:
if (other.gameObject.tag == "RedCollision")
Debug.Log("He's a Red Boy!");
gameObject.tag = "RedPlayer";
要:
if (other.gameObject.tag == "RedCollision") {
Debug.Log("He's a Red Boy!");
gameObject.tag = "RedPlayer";
}
如果你惹人注意的话,总是正确缩进你的东西。
对此进行测试:
if( X != 1 )
Debug.Log( "1" );
if( X == 2 )
Debug.Log( "2" );
else
Debug.Log( "3" );
和
if( X != 1 )
Debug.Log( "1" );
if( X == 2 )
Debug.Log( "2" );
else
Debug.Log( "3" );
注意一个空格差异
感谢此Python-ian缩进类型,如果您的X
等于2,则顶部示例将记录"1"
,然后记录"2"
,而底部将记录"3"