0907 배운 내용 (for, array)

null·2023년 9월 25일

Unity Study

목록 보기
5/60




private void

{

    for    ( 조건 ;  반복 ; 반복  )

    {

        print (결과값);

    }

}

using System.Collections;


---------------------------------------------------


// 배열 ==> 하나의 변수 이름으로 여러개의 데이터를 담는 '묶음형 변수'



public int[] numbers = new int[5];



//데이터타입[] 배열이름 = new(생성자키워드) 데이터타입[담을 데이터 갯수]

//배열에서 원하는 데이터를 찾기 위해 루프를 활용하는 것이 좋다

//for루프는 순서대로 배열의 요소를 뒤질 때 사용

//foreach루프는 순서와 상관없이 특정조건에 맞는 요소를 찾을 때 사용



public int[] myArray = new int[5];





void Start()

{

    

    print("numbers의 정체: " + numbers);



    numbers[0] = 0;

    numbers[1] = 10;

    numbers[2] = 200;

    numbers[3] = 3000;

    numbers[4] = 40000;



    for(int i = 0; i < numbers.Length; i++)

    {

        print(numbers[4]);

    }

    

}



private void OnMouseDown()

{

    /*

    for (int i = 0; i < numbers.Length; i ++)

    {

        print(numbers[3]);

    }

    */



    print("배열갯수: " + myArray.Length);

    print(myArray[myArray.Length - 1]);



}



// Update is called once per frame

void Update()

{

    

}

public GameObject[] boxes;



Material Box_Color;



int boxCount = 0;



// Start is called before the first frame update

void Start()

{

}



private void OnMouseDown()

{



    /*

    

    if (boxCount < boxes.Length)

    {

        boxCount++;

        print("현재 선택된 박스: " + boxCount);

    }



    if (boxCount == boxes.Length)

    {

        boxCount = 0;

    }



    boxes[boxCount].GetComponent<Renderer>().material.color = Color.red;



    */



    MakeAllRed();



    print("현재 선택된 박스: " + boxCount);

    boxes[boxCount].GetComponent<Renderer>().material.color = Color.blue;



    if (boxCount < 5)

    {

        boxCount++;

    }

    if (boxCount == 5)

    {

        boxCount = 0;

    }



}



void MakeAllRed()

{

    for (int count = 0; count < boxes.Length; count++)

    {

        boxes[count].GetComponent<Renderer>().material.color = Color.red;

    }

}



// Update is called once per frame

void Update()

{

    

}

0개의 댓글