I’m currently working on class called “Room” which has a bunch of “Connection” game objects. When I place another object from this class I would like to have them share the same Connection game object. This means I need to delete one of the connection objects and pass a reference of the remaining Connection object to the Room object that’s missing it. I’m not quite sure how to pass by reference in C#. Can someone point me in the right direction? If I can provide any more information to make things a little clearer don’t hesitate to ask and thank you for your help!
Hey,
Not sure this is exactly what you need but check it out ref keyword - C# reference | Microsoft Learn
And if you can, provide a code example
Unfortunately that’s giving me a 404 error. Unfortunately the code wont help much at this stage, The gist of it is I have two game objects, each of them spawns with a reference to two more game objects. I want to delete one of them and make both of the Room objects point to the remaining one.
//Merge the objects the connections represent so we can turn on off both sets of connections just by toggling this one connection.
//Then when we're finished delete one of the connections and set the saved connection as the other connection.
void Connect(Connection savedConnection, Connection destroyedConnection)
{
//Merge the object lists so we know what to turn on/off when we open or close our connection.
savedConnection.openObjects = savedConnection.openObjects.Concat(destroyedConnection.openObjects).ToArray();
savedConnection.closeObjects = savedConnection.closeObjects.Concat(destroyedConnection.closeObjects).ToArray();
//Delete one connection and replace it with a reference to our new connection
}
void SometOtherMethod()
{
Connection c1, c2;
Connect(ref c1, c2);
}
void Connect(ref Connection savedConnection, Connection destroyedConnection)
{
}
Due to the ref keyword, Connect method can now change the value of c1.
Is this what you were going for?
EDIT: Note that there’s also out keyword. It forces the method to do an assignment to the variable. out keyword - C# reference | Microsoft Learn
If that gets solved based on previous posts, that’s great.
If not, and you’re still looking, could you be a bit more explicit on which ones are destroyed and which ones are saved.
![]()
I’m really sorry about the delay in my responses. I wound up sick yesterday and couldn’t concentrate on my code. Ref seemed to be exactly what I needed, I’ll have to play with the rest of the level generator to see if it’s really working now but I think I have it. Thank you for all your help, I couldn’t find what I was looking for!
I’m not sure where the confusion on which object i’m destroying is coming from. I’m destroying the connection object from the non calling Room object. If I’m still missing the point let me know.
public void UpdateConnections(List<Room> adjacentRooms)
{
foreach(Room room in adjacentRooms)
{
foreach(Connection connection in room.connections)
{
foreach(Connection myConnection in connections)
{
//If we're within 1 unit in the X/Y direction we want to connect our objects.
if(Mathf.Abs(myConnection.gameObject.transform.position.x - connection.gameObject.transform.position.x) < 1)
{
if(Mathf.Abs(myConnection.gameObject.transform.position.x - connection.gameObject.transform.position.x) < 1)
{
Connection oldConnection = connection;
//Connect the two game objects and then discard them.
Connect(myConnection,ref oldConnection);
}
}
}
}
}
}
//Merge the objects the connections represent so we can turn on off both sets of connections just by toggling this one connection.
//Then when we're finished delete one of the connections and set the saved connection as the other connection.
void Connect(Connection savedConnection,ref Connection destroyedConnection)
{
//Merge the object lists so we know what to turn on/off when we open or close our connection.
savedConnection.openObjects = savedConnection.openObjects.Concat(destroyedConnection.openObjects).ToArray();
savedConnection.closeObjects = savedConnection.closeObjects.Concat(destroyedConnection.closeObjects).ToArray();
//overwrite the old connection
Destroy(destroyedConnection.gameObject);
destroyedConnection = savedConnection;
}
i just want help with nav meshing