Unité C #, NullReferenceException: objet de référence non définie à une instance d'un objet. niveau generaton

voix
-1

Erreur: NullReferenceException: objet de référence non définie à une instance d'un objet LevelGeneration.Move () (at actif / scripts / LevelGeneration.cs: 98) LevelGeneration.Update () (at actif / scripts / LevelGeneration.cs: 39)

Problème: Je crée un programme de génération de niveau suivant un tutoriel et même regardé le code source. il est identique

chaque fois que je frappe le lance génère quelques chambres et jette alors l'erreur ci-dessus. Je regardais à travers d'autres peuples des choses ici et ce n'est pas applicable à ma situation

La ligne: if (roomDetection.GetComponent () type = 1 && roomDetection.GetComponent () type = 3.!.!)

aide s'il vous plaît

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


}

}

Créé 14/02/2020 à 00:06
source utilisateur
Dans d'autres langues...                            

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