[스파르타 부트캠프] Array를 사용한 Index

피스타치오·2025년 3월 28일
0


사용한 프로그램

  • Visual Studio 2022

  • 메모
int[] arr = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
arr.OrderBy(x=>Random.Range(0f,7f)).ToArray();  

for (int i = 0; i < 16; i++)
{
    GameObject go = Instantiate (card,this.transform);

    float x = (i % 4) * 1.4f - 2.1f;
    float y = (i / 4) * 1.4f - 3.0f;

    go.transform.position = new Vector2(x,y);
    go.GetComponent<Card>().Setting(arr[i])
  • 사전강의를 들으며 신기했던 배열을 인덱스에 넣어 랜덤값으로 보여주기 위한 과정인데
int Idx;
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    
}

public void Setting(int number)
{
    Idx = number;
}

배열과 랜덤까지는 이해 했으나 이걸 따로 저장해서 랜덤값으로 표현하는 것을 보고 이러한 방법이 있구나 하고 새롭게 알게되었다.

0개의 댓글