C#将对象添加到List,只引用List中的一个对象

时间:2015-03-07 21:32:41

标签: c# unity3d

我正在向List添加对象但是在打印时它是同一个对象,即最后添加的对象。我知道它与引用有关,但无法找到如何解决它。在If段(“B”)我正在做Resouce.Load的新精灵,但是在“else”之后,正如你所看到的,我正在重复使用相同的预制件。

在>>>>>>>>>>>>>>>>>><<<<< #1>>>所有看起来都不错但是在>>>>>>>>>>>>>>>>>><<<< #2>>>只打印最后一个对象。

           List<GameObject> processedContainerGO = newList<GameObject>();

           foreach (GameObject fGO in theContainerList) {

                if (toBeFlippedCard_GO.tag.LastIndexOf ("B") != -1) { // Back is up - load front

                ...CODE

                }
                else { //

                    // GET THE CORRECT TAG NAME
                    convertedCardTag = CardsAndConversions.front2Back [toBeFlippedCard_GO.tag];

                    // LOAD THE BACK SIDE CARD OBJECT
                    flip_GO = Resources.Load("BackSide") as GameObject;

                    // SET CORRECT NAME AND TAG
                    flip_GO.transform.name = convertedCardTag;
                    flip_GO.transform.tag = convertedCardTag;
                }

                //
                flip_GO.transform.localScale = cardScaleVector;

                // POSITION NEW "FLIPPED" OBJECT ON SAME SPOT
                flip_GO.transform.position = originalPosition_v3;

                // PUT THE CORRECT ROTATION
                flip_GO.transform.rotation = originalRotation_Q;
                print ("Add 2 flip_GO: " + flip_GO); >>>>>>>>>>>>>>>><<< #1 >>>
                processedContainerGO.Add(flip_GO);

                // DESTROY THE CARD THAT SHOULD BE FLIPPED
                Destroy (GameObject.FindWithTag (toBeFlippedCard_GO.tag));

                // INSTANTIATE THE FLIPPED CARD
                temp_GameObject = Instantiate (flip_GO) as GameObject;

        }

        foreach (GameObject aGO in processedContainerGO) print ("processedContainer: " + aGO.tag); >>>>>>>>>>>>>>>><<< #2 >>>

0 个答案:

没有答案