एकता सी #, NullReferenceException: ऑब्जेक्ट संदर्भ एक वस्तु का एक उदाहरण के लिए सेट नहीं। स्तर generaton

वोट
-1

त्रुटि: NullReferenceException: ऑब्जेक्ट संदर्भ एक वस्तु LevelGeneration.Move () (आस्तियों / स्क्रिप्ट / LevelGeneration.cs पर: 98) का एक उदाहरण के लिए सेट नहीं LevelGeneration.Update () (आस्तियों पर / स्क्रिप्ट / LevelGeneration.cs: 39)

समस्या: मैं एक स्तर पीढ़ी कार्यक्रम एक ट्यूटोरियल के बाद और भी स्रोत कोड को देखा बनाने रहा हूँ। यह समान है

हर बार जब मैं इसे कुछ कमरों में उत्पन्न करता है चलाने मारा और फिर ऊपर बताए गए त्रुटि फेंकता है। मैं सामग्री यहां मौजूद अन्य लोगों के माध्यम से देखा है और यह मेरी स्थिति के लिए लागू नहीं है

रेखा: अगर (। roomDetection.GetComponent () प्रकार = 1 && roomDetection.GetComponent () प्रकार = 3!।!)

कृपया सहायता कीजिए

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


}

}

14/02/2020 को 00:06
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            

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