2014-07-16 3 views
-2

나는이 스크립트에 대해 약 몇 주간 붙어있다. 이 스크립트는 새 게임이 시작될 때마다 목록에있는 항목에 대한 스크립트가 있습니다. 왜 그런지 모르지만 항목 목록을 뒤섞어 놓을 수는 없습니다. 할 수 있으면 직접 도와주세요. 또한 문제가 될 수도있는 문제를 해결할 수있는 방법을 설명해주십시오. 아직 완전히 익숙하지 않습니다.내가 여기서 뭔가 잘못하고있다. Unity 3D

`using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class RayCasting : MonoBehaviour 
{ 
    public float pickupDistance; 
    public List<Item> items; 
    public List<Item> finalItems; 

    #region Unity 
    void Start() 
    { 
     Screen.lockCursor = true; 
     // Do a while loop until finalItems contains 5 Items 
     while (finalItems.Count < 5) { 
      Item newItem = items[Random.Range(0, items.Count)]; 
      if (!finalItems.Contains(newItem)) { 
       finalItems.Add(newItem); 
       } 
      items.Clear(); 
      } 
    } 
    void Update() 
    { 
     RaycastHit hit; 
     Ray ray = new Ray(transform.position, transform.forward); 
     if (Physics.Raycast(ray, out hit, pickupDistance)) 
     { 
      foreach(Item item in finalItems) 
      { 

       if(Input.GetMouseButtonDown(0)) { 
        if (item.gameObject.Equals(hit.collider.gameObject)) 
       { 
        numItemsCollected++; 
        item.Collect(); 
        break; 
         } 
       } 
      } 
     } 
    } 

    void OnGUI() 
    { 

     GUILayout.BeginArea(new Rect(130,400,100,130)); 
     { 
      GUILayout.BeginVertical(); 
      { 
     if (numItemsCollected < items.Count) 
     { 
      foreach (Item item in finalItems) 

         GUILayout.Label(string.Format("[{0}] {1}", items.Collected ? "" + items.password: " ", items.name)); 
     } 
     else 
     { 
      GUILayout.Label("You Win!"); 
     } 
      } 
      GUILayout.EndVertical(); 
    } 
     GUILayout.EndArea(); 
    } 
    #endregion 

    #region Private 
    private int numItemsCollected; 
    #endregion 
} 

[System.Serializable] 
public class Item 
{ 
    public string name; 
    public GameObject gameObject; 
    public int password; 

    public bool Collected { get; private set; } 



    public void Collect() 
    { 
     Collected = true; 
     gameObject.SetActive(false); 
    } 

    public void passwordNumber() 
    { 
     password = 0; 
     Collected = true; 
     gameObject.SetActive(false); 
    } 


} 

` 
+0

나는 정말로 당신의 문제가 무엇인지 이해하지 못한다. 1) 스크립트에서 수행 할 작업과 2) 예상하지 못한 스크립트에 대해 더 자세히 설명 할 수 있습니까? – Tom

+0

그러면 스크립트는 기본적으로 1-10의 항목 목록을 만듭니다. 목록에서 찾은 항목을 클릭 할 때마다 임의의 숫자가 표시되므로 수집하면 번호가 중요하지 않은 다른 작업을 수행하게됩니다. 새로운 게임이 시작될 때마다 아이템이 뒤섞이지 않는다는 것이 내 의견이다. 나는 당신이 게임을 시작할 때마다 똑같은 목록을 원하지 않는다. 조금 나아 졌어 ?? – Ghostdre

+0

내가 잘못했는지는 모르겠지만 항목 목록을 무작위로 추출하지는 않습니다. 나는 어디로 가야할 지 이해하지 못한다. 더 자세한 정보가 필요하시면 알려주세요. – Ghostdre

답변

0

이 예제는 너무 복잡하며 질문하는 것처럼 보이는 많은 관련이없는 코드가 있습니다. 기본 전략으로 문제가 있다고 생각되는 코드를 격리하고 문제를 해결 한 다음 더 큰 경우에 적용해야합니다.

예를 들어, 기본 논리에 적어도 하나 이상의 오류가 있습니다. Random.Range가 ( 포함)이므로 "index.Count-1"을 사용하지 않으면 범위를 벗어날 수 있습니다.

다음은 큰 "원본"목록에서 5 개의 임의 항목 목록을 만드는 예입니다. 당신은 당신의 코드에 이것을 적용 할 수 있어야한다 : 나는 내 댓글에 설명 된 이유, 일 수있는 items.Clear()을 제거 생각

using UnityEngine; 
using System.Collections.Generic; 

public class RandomList : MonoBehaviour { 

    public List <int> originals = new List<int>(); 
    public List <int> randomized = new List<int>(); 
    public int desiredNumberOfRandomInts = 5; 
    // Use this for initialization 
    void Awake() { 
    for(int i = 1; i <= 20; ++i) { 
     originals.Add(i); 
    } 
    while(randomized.Count < desiredNumberOfRandomInts) { 
     int randomSelection = originals[Random.Range(0, originals.Count-1)]; 
     if (!randomized.Contains(randomSelection)) { 
     randomized.Add(randomSelection); 
     } 
    } 
    } 
} 
0

.

while (finalItems.Count < 5) { 
    Item newItem = items[Random.Range(0, items.Count)]; 
    if (!finalItems.Contains(newItem)) { 
     finalItems.Add(newItem); 
    } 
} 

하지만 여전히, 난 당신이 이미 이미 items 곳의 목록을 가지고 있으리라 믿고있어 (그리고 목록> 5해야하기 때문에 당신은 루프 최소 5 배). 당신이하지 않으면 items.Count은 여전히 ​​0으로 유지되고 랜덤은 Random.Range(0,0)이됩니다.