3 years ago

Found a weird bug in a game I'm working on and don't know how to fix it.


I'm working on a game in Unity, and at the moment it's not much. There are a set of three doors you can open or close by clicking them. The doors are in an array, and I can detect which door is being clicked by taking the name of the object and using a foreach loop to figure out where in the array that object is. It will use that object's index as a tag to figure out which door to open. I programmed it so that the function will automatically default to 0 if the foreach loop fails. I was under the impression that it never fails, but for whatever reason it does. Sometimes, but only if you're moving around, the function will fail and default to 0, resulting in the program toggling the door with index 0 instead of the door that was actually clicked on. Does anyone know why this is happening?

Here's the full script. Let me know if you need more information.

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

public class ObjectInteraction1 : MonoBehaviour
{
    //Note to self: Always use GameObject arrays from now on. This is a game changer. Literally.

    public GameObject[] Doors;
    public GameObject[] DoorHandles;

    //This array of booleans determines two things about a door. Whether or not it's open, and whether or not it's
    //locked.

    public bool[,] doorStates = { { true, true, true }, { true, true, true } };

    public int value;

    public float animationTimer = 2;

    public LayerMask doorMask;

    private void Update()
    {
        Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));

        Physics.Raycast(ray, out RaycastHit hitData);

        //This single if statement will basically determine the entire game's interaction system. Simply pressing M1
        //will activate every single line of code below.

        if (Input.GetMouseButtonDown(0))
        {
            //This really weird bug keeps occuring and I have no idea how to fix it. Sometimes, for whatever reason,
            //this value will just reset to 0 whenever you click on a door, resulting in the wrong door opening. It's
            //a really confusing bug, but it seems to only occur when the player is moving. Hopefully it doesn't get
            //in the way of anyone's playthrough. It might give them a spook.

            value = Value(hitData.collider.name);

            if (Physics.Raycast(ray, 2.5f, doorMask) && animationTimer <= 0)
            {
                if (doorStates[1, value])
                {
                    if (doorStates[0, value])
                    {
                        Doors[value].GetComponent<Animator>().Play("DoorOpen");
                        DoorHandles[value].GetComponent<Animator>().Play("Handle");
                        doorStates[0, value] = false;
                    }
                    else if (!doorStates[0, value])
                    {
                        Doors[value].GetComponent<Animator>().Play("DoorClose");
                        doorStates[0, value] = true;
                    }

                    animationTimer = 2;
                }
            }
        }

        animationTimer -= Time.deltaTime;
    }

    //This function will basically find out what object you are currently clicking on, by looking through the array
    //of interactable objects and figuring out which one has the same name as the object you're clicking on.
    //When I got this idea I basically said to myself, "Yeah, this is big brain time."

    int Value(string objectName)
    {
        int value = 0;

        foreach (GameObject i in Doors)
        {
            if (i.name == objectName)
            {
                return value;
            }

            value++;
        }

        return 0;
    }
}

		
	


1 comment

Loading...

Next up

i've been read like a book

welp, we had a good run, time to pack things up

IT HAPPENED

IT ACTUALLY HAPPENED

Happy #WIPWednesday!

Are you working on a game?

Making some art?

Practicing a song?

Something else?

Tell us in the comments!

HOLY CRAP A VERIFIED YOUTUBER COMMENTED ON MY VIDEO

Happy #WIPWednesday!

Are you working on a game?

Making some art?

Practicing a song?

Something else?

Tell us in the comments!

haha underdeveloped mental maturity go brrrrr

@balmut is a Jolter to Watch! They post great game dev updates! Follow them before the quest ends on October 15 and you'll get Coins!

hm

mug