Unitatea C #, NullReferenceException: Referința obiect nu este setată la o instanță a unui obiect. generaton nivel

voturi
-1

Eroarea: NullReferenceException: Referința obiect nu este setată la o instanță a unui obiect LevelGeneration.Move () (la Active / Scripturi / LevelGeneration.cs: 98) LevelGeneration.Update () (la Active / Scripturi / LevelGeneration.cs: 39)

Problemă: Sunt crearea unui program de generare nivel ca urmare a unui program de instruire și chiar uitat la codul sursă. este identic

de fiecare dată când am lovit rulați-l generează câteva camere și apoi se aruncă eroarea enumerate mai sus. M-am uitat prin alte popoare lucruri aici și nu este aplicabil situația mea

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

te rog ajuta-ma

    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;
        }
    }


}

}

Întrebat 14/02/2020 la 00:06
sursa de către utilizator
În alte limbi...                            

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