Can't reactivate deactivated object from another script.

In my scene I have a parent room object. As children to that room object are a bunch of wall objects and a couple connection objects. Each connection object has a reference to the wall objects and two methods. The walls deactivate fine when I call my CloseConnection() method but never reactivate when I call my OpenConnection() method.

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

/// <summary>
/// Connection classes are used to hold information about where rooms can connect to other rooms
/// Connection classes also hold a list of objects used in opening/closing those connections.
/// </summary>
public class Connection : MonoBehaviour 
{
    //canOpen will be flagged as true when it's connected to another connection.
    public bool canOpen = false;
    public bool isOpen = false;

    public GameObject[] openObjects;
    public GameObject[] closeObjects;

    void Start()
    {
        //Every connection should start closed.
        CloseConnection();
    }

    //TODO: This isn't working. It works if we havent run CloseConnection yet, once we do though it wont work again.
    public void OpenConnection()
    {
        if(canOpen == true)
        {
            isOpen = true;
            foreach(GameObject activatedObject in openObjects)
            {
               
                activatedObject.SetActive(true);
                //Returns true every time
                Debug.Log(activatedObject.activeSelf);
            }
            foreach(GameObject deactivatedObject in closeObjects)
            {
                deactivatedObject.SetActive(false);
                //returns false every time.
                Debug.Log(deactivatedObject.activeSelf);
            }
        }
    }

    public void CloseConnection()
    {
        isOpen = false;
        foreach(GameObject deactivatedObject in openObjects)
        {
            deactivatedObject.SetActive(false);
        }
        foreach(GameObject activatedObject in closeObjects)
        {
            activatedObject.SetActive(true);
        }
    }
}

Couple of thoughts:

  • in open connection, use Debug.Log() to output the canOpen. Is it really true?

  • is CloseConnection being continously called (every frame) by something else? Put a Debug.Log() output in it to see

  • in each of those methods, use Debug.Log() to print out gameObject.name, see if that script is on the object you think it is.

Close connection is only ever called once, when the connection’s Start() function is called. As long as I’ve never called CloseConnection() OpenConnection() will work just fine. The debug.log below activatedObject.SetActive(true); returns false so it’s clearly getting into the foreach loop, it simply never activates the objects.

I read this post earlier, and tried to make sense of this & how it’s happening.
Where/when do you call these functions (outside of this script)? Some other code that may shed some light here?

I have a controller object with a script on it to generate the dungeon. All of the objects it creates are children to that controller object, after it’s done creating all of the objects it calls this function. It’s currently the only place OpenConnection() is called, I’m pretty sure it’s an issue with setting the object to activated/deactivated though.

//This itterates through each room and opens one connection.
    void ConnectDungeon()
    {
        //Get a list of rooms
        List<Room> worldRooms = new List<Room>();
        foreach (Room room in WorldTiles)
        {
            if (room != null && !worldRooms.Contains(room))
            {
                worldRooms.Add(room);
            }
        }


        //Loop for each room, pick a random connection and try and open it. If it fails keep picking a random connection and trying to open it.
        foreach (Room room in worldRooms)
        {
            Connection connection = room.connections[Random.Range(0, room.connections.Count)];
            bool hasOpenedConnection = false;
            while (hasOpenedConnection == false)
            {    
                //TODO: This could lead to an infinite loop if a room has no open connections. Maybe I should add them to a list randomly and then iterate through them.
                //If the connection can be opened open it.
                if (connection.canOpen == true && connection.isOpen == false)
                {
                    Debug.Log("Open Connection");
                    connection.OpenConnection();
                    hasOpenedConnection = true;
                }
                else //if not pick a different room and try again.
                {
                    connection = room.connections[Random.Range(0, room.connections.Count)];
                }
            }
           
        }
    }

I’d like to help more, but I feel at a total loss as to what’s the problem, based on what you’ve shown & described/explained.
If the project isn’t massive and you’d like some more help, you could post a download link and I could look at it…