El Error: NullReferenceException: referencia a objeto no establecida como una instancia de un objeto LevelGeneration.Move () (en activos / scripts / LevelGeneration.cs: 98) LevelGeneration.Update () (en Activos / Scripts / LevelGeneration.cs: 39)
Problema: Estoy creando un programa de generación de actividad dentro de un tutorial e incluso mirado el código fuente. que es idéntica
cada vez que me golpeó la funciono genera unas pocas habitaciones y luego lanza el error enumerado anteriormente. Miré a través de otros pueblos cosas aquí y no es aplicable a mi situación
La línea: if (. RoomDetection.GetComponent () type = 1 && roomDetection.GetComponent () type = 3!.!)
por favor ayuda
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;
}
}
}
}