inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

135

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

52

1

강의 추천해주세요

2

29

1

수업 잘 들었습니다.

2

19

2

맥유저 입장의 고려해주세요

2

52

1