Skip to content

The guy from youtube #1

@BurakAkgun34

Description

@BurakAkgun34

This is my mazenode.cs file

public enum NodeState
{
    Available,
    Current,
    Completed
}

public class mazenode : MonoBehaviour
{
    [SerializeField] GameObject[] walls;
    [SerializeField] MeshRenderer floor;

  
    public void Removewall(int walltoremove)
    {
        walls[walltoremove].gameObject.SetActive(false);
   
    }
  

    public void SetState(NodeState state)
    {
        switch (state)
        {
            case NodeState.Available:
                floor.material.color = Color.white;
                break;
            case NodeState.Current:
                floor.material.color = Color.yellow;
                break;
            case NodeState.Completed:
                floor.material.color = Color.blue;
                break;

        }
    }    
}

This is my mazegenerator file

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

public class mazegenerator : MonoBehaviour
{
    [SerializeField] mazenode nodePrefab;
    [SerializeField] Vector2Int mazeSize;


    private void Start()
    {
        StartCoroutine(GenerateMaze(mazeSize));

    }

    IEnumerator GenerateMaze(Vector2Int size)
    {
        List<mazenode> nodes = new List<mazenode>();

        //This is creating nodes
        for (int x = 0; x < size.x; x++)
        {
            for (int y = 0; y < size.y; y++)

            {
                Vector3 nodePos = new Vector3(x - (size.x / 2f), 0, y - (size.y / 2f));
                mazenode newNode = Instantiate(nodePrefab, nodePos, Quaternion.identity, transform);
                nodes.Add(newNode);

                yield return null;
            }

        }



        List<mazenode> currentPath = new List<mazenode>();
        List<mazenode> completedNodes = new List<mazenode>();


        // Choose starting node at random
        currentPath.Add(nodes[Random.Range(0, nodes.Count)]);
        currentPath[0].SetState(NodeState.Current);
        


        while (completedNodes.Count < nodes.Count)
        {
            //Check nodes next to the current node
            List<int> posnexNodes = new List<int>();
            List<int> posdirectionNodes = new List<int>();


            int currentNodeIndex = nodes.IndexOf(currentPath[currentPath.Count - 1]);
            int currentNodeX = currentNodeIndex / size.y;
            int currentNodeY = currentNodeIndex % size.y;

            
            if (currentNodeX < size.x - 1) //checking if it's on the rigt wall
            {
                if (!completedNodes.Contains(nodes[currentNodeIndex + size.y]) &&
                    !currentPath.Contains(nodes[currentNodeIndex + size.y])) 


                {
                    posdirectionNodes.Add(1);
                    posnexNodes.Add(currentNodeIndex + size.y);
                }
            }

            if (currentNodeX > 0)  // Check node to left of the current node
            {
                 
                if(!completedNodes.Contains(nodes[currentNodeIndex - size.y]) &&
                        !currentPath.Contains(nodes[currentNodeIndex - size.y]))
                    {
                    posdirectionNodes.Add(2);
                    posnexNodes.Add(currentNodeIndex - size.y);
                    }
            }

            if (currentNodeY < size.y - 1) //Check node above the current node
            {
                if (!completedNodes.Contains(nodes[currentNodeIndex + 1]) &&
                    !currentPath.Contains(nodes[currentNodeIndex + 1]))
                {
                    posdirectionNodes.Add(3);
                    posnexNodes.Add(currentNodeIndex + 1);

                }
            }

            if(currentNodeY > 0)  //Check node below current node
            {
                if (!completedNodes.Contains(nodes[currentNodeIndex - 1]) &&
                     !currentPath.Contains(nodes[currentNodeIndex -1]))
                {
                    posdirectionNodes.Add(4);
                    posnexNodes.Add(currentNodeIndex - 1);
                }
                    
            }


            if(posdirectionNodes.Count > 0)  //Choose next node
            {
                int chosenDirection = Random.Range(0, posdirectionNodes.Count);
                mazenode chosenNode = nodes[posnexNodes[chosenDirection]];


               switch (posdirectionNodes[chosenDirection])
                {
                    case 1:
                        chosenNode.Removewall(1);
                        currentPath[currentPath.Count - 1].Removewall(0);
                        break;
                    case 2:
                        chosenNode.Removewall(0);
                        currentPath[currentPath.Count - 1].Removewall(1);
                        break;
                    case 3:
                        chosenNode.Removewall(3);
                        currentPath[currentPath.Count - 1].Removewall(2);
                        break;
                    case 4:
                        chosenNode.Removewall(2);
                        currentPath[currentPath.Count - 1].Removewall(3);
                        break;
         
                }
              
                currentPath.Add(chosenNode);
                chosenNode.SetState(NodeState.Current);
            }

            else
            {
                completedNodes.Add(currentPath[currentPath.Count - 1]);

                currentPath[currentPath.Count - 1].SetState(NodeState.Completed);
                currentPath.RemoveAt(currentPath.Count -1);

            }

            yield return new WaitForSeconds(0.05f);

        }
    }

    


}

There is no errors in my compiler, when it runs all it does is give me the following image:

With completed code

If I take out these lines of code in both the mazenode.cs and mazegenerator.cs the following image is what I get


 public void Removewall(int walltoremove)
    {
        walls[walltoremove].gameObject.SetActive(false);
   
    }

 switch (posdirectionNodes[chosenDirection])
                {
                    case 1:
                        chosenNode.Removewall(1);
                        currentPath[currentPath.Count - 1].Removewall(0);
                        break;
                    case 2:
                        chosenNode.Removewall(0);
                        currentPath[currentPath.Count - 1].Removewall(1);
                        break;
                    case 3:
                        chosenNode.Removewall(3);
                        currentPath[currentPath.Count - 1].Removewall(2);
                        break;
                    case 4:
                        chosenNode.Removewall(2);
                        currentPath[currentPath.Count - 1].Removewall(3);
                        break;
         
                }

without the remove wall

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions