커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
3-6 레벨이 올라가도 fatCat이 등장하지 않습니다.
undefined주차
북마크
유*민
댓글
3
추천
0
조회수
24
조회수
24
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


강의에 따라 레벨 3이 되면 뚱뚱한 고양이(fatCat)이 등장하도록 코드를 짰는데 레벨 6이 되도록 fatCat이 나오지 않습니다.

cat.cs의 문제인가 하여 if (level == 1 ) 이나 else if (level >= 2) 아래에 Instantiate(fatCat);를 넣어봤더니 그때는 잘 등장하고, else if ( level >= 3 ) 의 아래에 넣으면 노말캣만 나옵니다.

(+추가)

해적고양이를 넣을 때도 동일한 문제가 발생합니다.

level >=2 에 넣으면 해적고양이가 정상적으로 나오나 level >=3, level>=4 에 넣으면 등장하지 않습니다.


보고계신 화면 전체를 캡처해 주시면, 튜터님들이 빠르게 상황을 이해할 수 있어요.

게임매니저인스펙터

fatCat 프리팹 인스펙터



작성한 코드 및 에러 메세지

//Game Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class gameManager : MonoBehaviour
{
    public GameObject dog;
    public GameObject food;
    public GameObject normalCat;
    public GameObject fatCat;
    public GameObject retryBtn;
    public Text levelText;
    public GameObject levelFront;

    public static gameManager I;

    int level = 0;
    int cat = 0;

    void Awake()
    {
        I = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("makeFood", 0.0f, 0.1f);
        InvokeRepeating("makeCat", 0.0f, 1.0f);
    }

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

    void makeFood()
    {
        float x = dog.transform.position.x;
        //dog의 x좌표를 구한다.
        float y = dog.transform.position.y;
        Instantiate(food, new Vector3(x, y, 0), Quaternion.identity);
        //Quaternion.identity: 회전이 없다.
    }
    void makeCat()
    {
        Instantiate(normalCat);
        if (level == 1)
        {
            float p = Random.Range(0, 10);
            if (p < 2) Instantiate(normalCat);
            

        }
        else if (level >= 2)
        {
            float p = Random.Range(0, 10);
            if (p < 5) Instantiate(normalCat);
            
        }
        else if (level >=3 )
        {
            float p = Random.Range(0, 10);
            if (p < 6) Instantiate(normalCat);
            Instantiate(fatCat);

        }
    }
    public void gameOver()
    {
        retryBtn.SetActive(true);
        Time.timeScale = 0.0f;
    }

    public void addCat()
    {
        cat += 1;
        level = cat / 5;

        levelText.text = level.ToString();
        levelFront.transform.localScale = new Vector3((cat - level * 5) / 5.0f, 1.0f, 1.0f);
    }
}

Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.

Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!


//cat.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cat : MonoBehaviour
{
    float full = 5f;
    float energy = 0f;
    bool isFull = false;
    public int type;
    //normalCat과 fatCat을 타입으로 구분한다.

    // Start is called before the first frame update
    void Start()
    {
        float x = Random.Range(-8.5f, 8.5f);
        float y = 30.0f;
        transform.position = new Vector3(x, y, 0);

        if (type == 1)
        {
            full = 10.0f;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if ( energy < full )
        {
            if (type == 0 )
            {
                transform.position += new Vector3(0f, -0.04f, 0f);
            }
            else if (type == 1)
            {
                transform.position += new Vector3(0f, -0.02f, 0f);
            }

            if (transform.position.y < -16f)
            {
                //GameOver;
                gameManager.I.gameOver();
            }
        }
        else
        {
            if (transform.position.x > 0)
            {
                transform.position += new Vector3(0.05f, 0.0f, 0.0f);
            }
            else
            {
                transform.position += new Vector3(-0.05f, 0f, 0f);

            }
            Destroy(gameObject, 3.0f);
        }
    }
    void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.gameObject.tag == "food")
        {
            if ( energy < full )
            {
                //Debug.Log("아야!");
                energy += 1.0f;
                Destroy(coll.gameObject);

                gameObject.transform.Find("hungry/Canvas/front").transform.localScale = new Vector3(energy / full, 1.0f, 1.0f);
            }
            else
            {
                //Debug.Log("배불러");
                if (isFull == false)
                    //지금 배가 부른가?
                {
                    gameManager.I.addCat();
                    gameObject.transform.Find("hungry").gameObject.SetActive(false);
                    gameObject.transform.Find("full").gameObject.SetActive(true);

                    isFull = true;
                }
                

                
            }
        }
        
    }
}




취소
 공유
취소
댓글 0
댓글 알림
나의얼굴