C# 只有最后一个游戏对象在循环后被重命名?
我将所有游戏对象保存在一个.txt文件中。当我尝试加载带有文本文件的地图时,除了gameobject.name之外,一切都正常工作。所有对象都放置在正确的位置,并带有良好的标记。这里是代码的一部分C# 只有最后一个游戏对象在循环后被重命名?,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我将所有游戏对象保存在一个.txt文件中。当我尝试加载带有文本文件的地图时,除了gameobject.name之外,一切都正常工作。所有对象都放置在正确的位置,并带有良好的标记。这里是代码的一部分 void OnMouseDown(){ if (GlobalManager.MapName != "") { string path = @"c:\Users\Ashes\Desktop\Rebel2\" + GlobalManager.MapName + ".txt";
void OnMouseDown(){
if (GlobalManager.MapName != "") {
string path = @"c:\Users\Ashes\Desktop\Rebel2\" + GlobalManager.MapName + ".txt";
if (loadedMap == GlobalManager.MapName) {
txtMessage2 = "This map is already open";
StartCoroutine (Message1 ());
}
if (!File.Exists (path)) {
txtMessage2 = "This map name not exist, place object to create the map";
StartCoroutine (Message1 ());
}
if (File.Exists (path)) {
if (loadedMap != GlobalManager.MapName) {
{
loadedMap = GlobalManager.MapName;
GameObject[] Tree = GameObject.FindGameObjectsWithTag ("Tree");
foreach (GameObject item in Tree) {
Destroy (item);
}
GameObject[] House = GameObject.FindGameObjectsWithTag ("House");
foreach (GameObject item in House) {
Destroy (item);
}
GameObject[] Road = GameObject.FindGameObjectsWithTag ("Road");
foreach (GameObject item in Road) {
Destroy (item);
}
GameObject[] Farm = GameObject.FindGameObjectsWithTag ("Farm");
foreach (GameObject item in Farm) {
Destroy (item);
}
GameObject[] Wall = GameObject.FindGameObjectsWithTag ("Wall");
foreach (GameObject item in Wall) {
Destroy (item);
}
}
using (StreamReader sr = File.OpenText(path)) {
string s = "";
while ((s = sr.ReadLine()) != null) {
overLoadtxt = s;
string[] splitArray = overLoadtxt.Split (' ');
loadStr1 = splitArray [0];
loadStr2 = splitArray [1];
loadStr3 = splitArray [2];
loadStr4 = splitArray [3];
loadStr5 = splitArray [4];
loadStr6 = splitArray [5];
if (loadStr2 == "House") {
Instantiate (House);
if (loadStr6 == "LuxHouse") {
House.GetComponent<SpriteRenderer> ().sprite = img1;
}
if (loadStr6 == "House1") {
House.GetComponent<SpriteRenderer> ().sprite = img2;
}
if (loadStr6 == "Greentex") {
House.GetComponent<SpriteRenderer> ().sprite = img3;
}
House.tag = "House";
House.transform.localScale = new Vector3 (0.5f, 0.5f, 0);
House.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
House.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intHouseName ++;
House.name = loadStr1;
Debug.Log (House.name);
}
if (loadStr2 == "Road") {
Instantiate (Road);
if (loadStr6 == "dirthroad") {
Road.GetComponent<SpriteRenderer> ().sprite = img4;
}
Road.tag = "Road";
Road.transform.localScale = new Vector3 (0.5f, 0.3f, 0);
Road.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
Road.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intRoadName ++;
Road.name = loadStr1;
Debug.Log (Road.name);
}
}
}
}
}
}
}
debug.log显示正确的内容,house1,house2,road1,house3。。。但只有最后一个gameobject.name应用了更改
void OnMouseDown(){
if (GlobalManager.MapName != "") {
string path = @"c:\Users\Ashes\Desktop\Rebel2\" + GlobalManager.MapName + ".txt";
if (loadedMap == GlobalManager.MapName) {
txtMessage2 = "This map is already open";
StartCoroutine (Message1 ());
}
if (!File.Exists (path)) {
txtMessage2 = "This map name not exist, place object to create the map";
StartCoroutine (Message1 ());
}
if (File.Exists (path)) {
if (loadedMap != GlobalManager.MapName) {
{
loadedMap = GlobalManager.MapName;
GameObject[] Tree = GameObject.FindGameObjectsWithTag ("Tree");
foreach (GameObject item in Tree) {
Destroy (item);
}
GameObject[] House = GameObject.FindGameObjectsWithTag ("House");
foreach (GameObject item in House) {
Destroy (item);
}
GameObject[] Road = GameObject.FindGameObjectsWithTag ("Road");
foreach (GameObject item in Road) {
Destroy (item);
}
GameObject[] Farm = GameObject.FindGameObjectsWithTag ("Farm");
foreach (GameObject item in Farm) {
Destroy (item);
}
GameObject[] Wall = GameObject.FindGameObjectsWithTag ("Wall");
foreach (GameObject item in Wall) {
Destroy (item);
}
}
using (StreamReader sr = File.OpenText(path)) {
string s = "";
while ((s = sr.ReadLine()) != null) {
overLoadtxt = s;
string[] splitArray = overLoadtxt.Split (' ');
loadStr1 = splitArray [0];
loadStr2 = splitArray [1];
loadStr3 = splitArray [2];
loadStr4 = splitArray [3];
loadStr5 = splitArray [4];
loadStr6 = splitArray [5];
if (loadStr2 == "House") {
Instantiate (House);
if (loadStr6 == "LuxHouse") {
House.GetComponent<SpriteRenderer> ().sprite = img1;
}
if (loadStr6 == "House1") {
House.GetComponent<SpriteRenderer> ().sprite = img2;
}
if (loadStr6 == "Greentex") {
House.GetComponent<SpriteRenderer> ().sprite = img3;
}
House.tag = "House";
House.transform.localScale = new Vector3 (0.5f, 0.5f, 0);
House.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
House.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intHouseName ++;
House.name = loadStr1;
Debug.Log (House.name);
}
if (loadStr2 == "Road") {
Instantiate (Road);
if (loadStr6 == "dirthroad") {
Road.GetComponent<SpriteRenderer> ().sprite = img4;
}
Road.tag = "Road";
Road.transform.localScale = new Vector3 (0.5f, 0.3f, 0);
Road.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
Road.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intRoadName ++;
Road.name = loadStr1;
Debug.Log (Road.name);
}
}
}
}
}
}
}
我对这个机械师一无所知。不管怎样,你故意放了重复的花括号?//如果已加载贴图!=GlobalManager.MapName{{是的,我认为你必须使用House的元素,而不是House本身。House是什么?也就是说,你声明它是什么?如果它是一个游戏对象,那么你实际上要做的是替换循环中对游戏对象的引用,而不是创建新对象。Road可能也是这样。如果你能发布整个脚本,我可能会帮你算计out@Venkat在Axiom Studios感谢您的回复,该项目目前更为先进。House是一个公共游戏对象House;。我不引用,我为文本文件中的每一行实例化一个新游戏对象。它是一个游戏内地图编辑器。我拖放游戏对象来创建地图,每次拖放后,文本中会保存一行像这样的文件,Farm12 Farm-0.16-0.89 1 1 1 0 Farm1 UnityEngine.Sprite。
void OnMouseDown(){
if (GlobalManager.MapName != "") {
string path = @"c:\Users\Ashes\Desktop\Rebel2\" + GlobalManager.MapName + ".txt";
if (loadedMap == GlobalManager.MapName) {
txtMessage2 = "This map is already open";
StartCoroutine (Message1 ());
}
if (!File.Exists (path)) {
txtMessage2 = "This map name not exist, place object to create the map";
StartCoroutine (Message1 ());
}
if (File.Exists (path)) {
if (loadedMap != GlobalManager.MapName) {
{
loadedMap = GlobalManager.MapName;
GameObject[] Tree = GameObject.FindGameObjectsWithTag ("Tree");
foreach (GameObject item in Tree) {
Destroy (item);
}
GameObject[] House = GameObject.FindGameObjectsWithTag ("House");
foreach (GameObject item in House) {
Destroy (item);
}
GameObject[] Road = GameObject.FindGameObjectsWithTag ("Road");
foreach (GameObject item in Road) {
Destroy (item);
}
GameObject[] Farm = GameObject.FindGameObjectsWithTag ("Farm");
foreach (GameObject item in Farm) {
Destroy (item);
}
GameObject[] Wall = GameObject.FindGameObjectsWithTag ("Wall");
foreach (GameObject item in Wall) {
Destroy (item);
}
}
using (StreamReader sr = File.OpenText(path)) {
string s = "";
while ((s = sr.ReadLine()) != null) {
overLoadtxt = s;
string[] splitArray = overLoadtxt.Split (' ');
loadStr1 = splitArray [0];
loadStr2 = splitArray [1];
loadStr3 = splitArray [2];
loadStr4 = splitArray [3];
loadStr5 = splitArray [4];
loadStr6 = splitArray [5];
if (loadStr2 == "House") {
Instantiate (House);
if (loadStr6 == "LuxHouse") {
House.GetComponent<SpriteRenderer> ().sprite = img1;
}
if (loadStr6 == "House1") {
House.GetComponent<SpriteRenderer> ().sprite = img2;
}
if (loadStr6 == "Greentex") {
House.GetComponent<SpriteRenderer> ().sprite = img3;
}
House.tag = "House";
House.transform.localScale = new Vector3 (0.5f, 0.5f, 0);
House.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
House.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intHouseName ++;
House.name = loadStr1;
Debug.Log (House.name);
}
if (loadStr2 == "Road") {
Instantiate (Road);
if (loadStr6 == "dirthroad") {
Road.GetComponent<SpriteRenderer> ().sprite = img4;
}
Road.tag = "Road";
Road.transform.localScale = new Vector3 (0.5f, 0.3f, 0);
Road.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
Road.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
GlobalManager.intRoadName ++;
Road.name = loadStr1;
Debug.Log (Road.name);
}
}
}
}
}
}
}