inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스크립트 에러로 실행이 안돼요

137

w2213

작성한 질문수 1

0

All compiler errors have to be fixed before you can enter playmode!

UnityEditor.SceneView:ShowCompileErrorNotification ()

찾아보니까 스크립트에서 잘못 작성해서 나온 에러 라는데 어디가 틀린것인지 모르겠어요..

도와주세요

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

public class BirdJump : MonoBehaviour
{
    Rigidbody2D rb;
    public float jumpPower;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Debug.Log("start");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)){ // 마우스를 클릭 했을 때
            rb.velocity = Vector2.up * jumpPower; // (0, 1) , vector2 = x, y
        }

        private void OnCollisionEnter2D(Collision2D other)
        {
            SceneManager.LoadScene("GameOverScane");
        }
    }
}

답변 1

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다.

w2213님, 스크립트에 작성된 코드를 확인해보니, OnCollisionEnter2D 메소드가 Update 메소드 내에 정의되어 있는 것이 문제입니다. OnCollisionEnter2D 메소드는 Update 메소드 밖으로 이동시켜야 합니다. 또한 SceneManager.LoadScene을 사용하기 위해서는 using UnityEngine.SceneManagement;을 추가해야 합니다. 아래와 같이 수정하면 에러를 해결하실 수 있습니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // 이 부분을 추가하세요.

public class BirdJump : MonoBehaviour
{
    Rigidbody2D rb;
    public float jumpPower;
    
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Debug.Log("start");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)){ // 마우스를 클릭 했을 때
            rb.velocity = Vector2.up * jumpPower; // (0, 1) , vector2 = x, y
        }
    }

    // OnCollisionEnter2D는 Update 메소드 밖에 위치해야 합니다.
    private void OnCollisionEnter2D(Collision2D other)
    {
        SceneManager.LoadScene("GameOverScene");
    }
}

이렇게 수정 후 스크립트를 저장하고 Unity 에디터로 돌아가시면, 이제 Playmode에 들어가실 수 있을 겁니다.

강의 수강 후 포트폴리오 준비 방향에 대해 조언 부탁드립니다.

2

47

1

시스템 아키텍처를 강의에 나온 것 처럼 그리고 싶은데 공유 가능할까요?

1

48

2

포트폴리오에 대한 질문이 있습니다!

1

45

2

7회 3유형 2번문제 질문

1

26

2