Enhet C #, NullReferenceException: Objekt referanse ikke er satt til en instans av et objekt. nivå generaton

stemmer
-1

Feil: NullReferenceException: Objekt referanse ikke er satt til en instans av et objekt LevelGeneration.Move () (ved Midler / skript / LevelGeneration.cs: 98) LevelGeneration.Update () (ved Assets / skript / LevelGeneration.cs: 39)

Problem: Jeg skaper et nivå generasjon program å følge en tutorial og selv så på kildekoden. det er identisk

hver gang jeg traff kjøre den genererer noen få rom og deretter kaster feilen ovenfor. Jeg så gjennom andre folks ting her, og det er ikke aktuelt å min situasjon

Linjen: if (. RoomDetection.GetComponent () type = 1 && roomDetection.GetComponent () type = 3!.!)

Vennligst hjelp

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

    public class LevelGeneration : MonoBehaviour
    {

public Transform[] startingPositions; //collects starting positions

public GameObject[] rooms; //index 0 --> LR, index 1 --> LRB, index 2--LRBT, index 3 --> RLT

private int direction; //direction indication variable
public float moveAmount; //move amount indication

private float timeBtwRoom;
public float startTimeBtwRoom = 0.25f;

public float minX; //these contain the generation to a designated area
public float maxX;
public float minY;
private bool stopGeneration;

public LayerMask room;

private void Start()
{
    int randStartingPos = Random.Range(0, startingPositions.Length); //detects length
    transform.position = startingPositions[randStartingPos].position; //moves the position
    Instantiate(rooms[0], transform.position, Quaternion.identity); //instatiate starti

    direction = Random.Range(1, 6);
}

private void Update()
{
    if (timeBtwRoom <= 0 && stopGeneration == false) 
    {
        Move(); //level generation moves in a random direction and spawns a room
        timeBtwRoom = startTimeBtwRoom;
    }
    else
    {
        timeBtwRoom -= Time.deltaTime;
    }
}

private void Move() // where the levels should be generated 
{
    if ( direction == 1 || direction == 2) //move right
    {
        if (transform.position.x < maxX) //checks if the direction isn't at max X
        {

            Vector2 newPos = new Vector2(transform.position.x + moveAmount, transform.position.y);

            transform.position = newPos;

            int rand = Random.Range(0, rooms.Length);
            Instantiate(rooms[rand], transform.position, Quaternion.identity);

            direction = Random.Range(1, 6);
            if (direction == 3)
            {
                direction = 2;
                //prevents rooms from spawning on top of each other
            }
            else if (direction == 4) {
                direction = 5;
            }
        }
        else //if the direction is past max X move down
        {
            direction = 5;
        }

    }
    else if (direction == 3 || direction == 4) //move left 
    {
        if (transform.position.x > minX) //only move left is the current transform position is less then x
        {
            Vector2 newPos = new Vector2(transform.position.x - moveAmount, transform.position.y);
            transform.position = newPos;
            direction = Random.Range(3, 6);
        }
        else //else move down
        {
            direction = 5;
        }

    }
    else if (direction == 5)// move down
    {
        if (transform.position.y > minY)
        {
            Collider2D roomDetection = Physics2D.OverlapCircle(transform.position, 3, room); //to set a layer mask
            Debug.Log(roomDetection);
            if (roomDetection.GetComponent<RoomType>().type !=1 && roomDetection.GetComponent<RoomType>().type != 3) //this is where the error is thrown

            {
                roomDetection.GetComponent<RoomType>().RoomDestruction();

                int randBottomRoom = Random.Range(1, 4);
                if(randBottomRoom == 2)
                {
                    randBottomRoom = 1;
                }
                Instantiate(rooms[randBottomRoom], transform.position, Quaternion.identity);

            }

            Vector2 newPos = new Vector2(transform.position.x, transform.position.y - moveAmount);
            transform.position = newPos;

            int rand = Random.Range(2, 4);
            Instantiate(rooms[rand], transform.position, Quaternion.identity);

            direction = Random.Range(1, 6);
        }
        else
        {
            stopGeneration = true;
        }
    }


}

}

Publisert på 14/02/2020 klokken 00:06
kilden bruker
På andre språk...                            

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more