2017-11-12 4 views
0

각 플레이어가 게임을 시작하기 전에 영웅을 선택하고 선택한 영웅을 전투장으로로드하는 여러 플레이어 용 게임이 있습니다.UNITY C# : 사용자 선택에 따라 캐릭터 프리 패브를 생성합니다.

나는 선수 내가 문자를 산란을 위해이 방법의 정확한 숫자에 산란을하기 위해 인스턴스를 얻기에 작은 문제가 : 선택된 영웅 카드의 금액에 대한

private void Placement() 
    { 
       for (int i = 0; i < SelectedCards.Count; i++) 
       { 
        for (int t = 0; t < AvailableHeroes.Count; t++) 
         { 
       if (AvailableHeroes[t].name == SelectedCards[i].name) 
       { 
        Debug.Log(AvailableHeroes[t]); 
         // Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation); 
       } 
       { 
       } 
      } 
     } 
    } 

이 스크립트를 확인하고에 넣는다 (prefabs)에서 선택할 수있는 모든 영웅을 가지고 내 목록. debug.log는 올바른 영웅 만 호출된다는 것을 보여줍니다. 인스턴스화는 선택한 금액 대신 영웅의 전리품을 산란시킵니다.

{ 

    private int playerSize;      //amount of choices for card selection 

    private GameManager GM; 
    [Header("Lists for Spawning in Heroes")] 
    public List<GameObject> SelectedCards; 
    public List<GameObject> AvailableHeroes; 
    public List<Transform> PlayerSpawnLocation; 
    [Header("Canvas used for the game")] 
    public Transform GameCanvas; 
    public Transform CharacterCanvas; 

    //When scene starts it takes how many players will be picking a card. 
    void Start() 
    { 
     //connects this script with gamenmanager to be able to manipulate the cameras 
     GM = GameObject.Find("GameManager").GetComponent<GameManager>(); 
     //gets playersize information from main menu selection 
     PlayerPrefs.GetInt("PlayerSize"); 
     playerSize = PlayerPrefs.GetInt("PlayerSize"); 
     SelectedCards = new List<GameObject>(); 
     //enables/disables correct canvas not to cause any problems when we initiate this scene 
     GameCanvas.gameObject.SetActive(false); 
     CharacterCanvas.gameObject.SetActive(true); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (playerSize <= 0) 
     { 
      Placement(); 
      GM.CharacterSelectionCamera.enabled = false; 
      GameCanvas.gameObject.SetActive(true); 
      CharacterCanvas.gameObject.SetActive(false); 
      GM.BattleCamera.enabled = true; 
     } 
    } 

    public void PlayerSelected(int cardPicked) 
    { 
     playerSize -= cardPicked; 
    } 

    private void Placement() 
    { 
       for (int i = 0; i < SelectedCards.Count; i++) 
       { 
        for (int t = 0; t < AvailableHeroes.Count; t++) 
         { 
       if (AvailableHeroes[t].name == SelectedCards[i].name) 
       { 
        Debug.Log(AvailableHeroes[t]); 
         // Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation); 
       } 
       { 
       } 
      } 
     } 
    } 
} 

은 내가이 문제 하겠어 어디에 누군가가 설명 할 수 있기를 바랍니다 : 명확성을 위해

나는 전체 클래스를 연결합니다. 감사합니다,

+0

멀티 플레이어 게임의 경우 필요한 모든 것은 다음과 같습니다. https://www.youtube.com/watch?v=2cYfGT2HK0g –

답변

0

나는 대답을 얻었습니다. 나는 단지 일하는 것에 지치고 명백한 것을 볼 수 없었을 것이라고 생각합니다. 궁금해하는 사람들을 위해 어떤 솔루션을하는 방법은 각 프레임에 따라서는 끊임없이 그것을 1 만들기 코 루틴을 수정하고 당신이 당신의 초기 배치 2 사용 한 후에 다음 반환하는 두 가지 방법이 있습니다 객체를 생성하는 것을 계속 호출되는 입니다 업데이트시 부울을 사용하여 플레이어 크기를 확인할뿐만 아니라 스폰 여부와 상관없이 get 메서드가 호출 된 후 부울을 false로 설정합니다.

0

나는 업데이트 기능 부분조차 몰랐다. 시작 기능에서 PlayerPrefs.GetInt ("PlayerSize"); 값이 아무 데나 저장되지 않기 때문에 아무 것도하지 않습니다.

+0

잠시만 여기에 있습니다. 두 번째 지점 인 playerSize는 실제로 다른 장면의 다른 스크립트에 저장됩니다. 내 메인 메뉴의 게임에서 총 플레이어 수를 선택하면 재생중인 플레이어의 수에 따라 스폰되는 플레이어 캐릭터를 관리하는이 장면으로 넘어갑니다. – snake555510

+0

아니요, 그 부분을 이해합니다. "PlayerPrefs.GetInt ("PlayerSize ");" 이 줄은 그 자체로 아무 것도하지 않습니다. "playerSize = PlayerPrefs.GetInt ("PlayerSize ");" 이 행은 실제로 값을 저장하고 있습니다. 그다지 많지는 않지만 미래에 코드를 도울 수 있습니다. –