Any ideas why this won't work?

Once again, I find myself struggling with these portals.

I am trying to link two portals in the same scene, so that the player can climb up a ladder:


when I walk through the lower level portal, instead of sending me to the upper floor, it just returns me to the first portal spawn. Earlier, I had the same issue but it was because I accidentally placed the spawn inside of the portal trigger, and that does not appear to be the case this time around.

I really don’t get it at this point. I feel like every time I drop a portal in the scene, the logic works completely differently than the last time.

There are no errors or warnings. Sometimes it does warp to the upper level, but I’ve only gotten it to do so twice, and I have no idea why it worked those times. Also, sometimes it warps me back to the initial player spawn point or to a random location nowhere near the portal.


Could the issue be with the navmesh itself? it looks like it’s mapped itself out just fine in scene view. I’ve had portals not work before because they weren’t interacting with the navmesh properly, but in those cases it at least gave me an exception error indicating the issue was with the navmesh.

The trouble lies in the Destination field of the portal. When we’re using just one variable to represent both the source and destination, then the first destination found in the scene that matches the portal destination will always win.

Scene 1: Portal destination A engaged, wanting to link to the other Portal Destination A
It -=looks=- like the if(other == this) line should filter out the first portal because that’s the one we’re using but what’s really happening behind the scenes is that calling portal is moved into the DontDestroyOnLoad bucket, a new Portal A is created when the scene loads, and this is the one that gets picked up by the calling Portal. Since the scene loaded, this new portal is != the calling portal, even though it’s in the exact same place. As instances, they are two distinct entities. This means the desired destination portal never actually gets called.

The Solution:
Add another field to Portal, call it Source. Now you’ll have a Source and a Destination field in the portal. When comparing the list of Portals, compare this.Destination with other.Source

Now you can set the first portal to Source A, Destination B (this portal is “A”, and you want to go to “B”). The second portal should now be Source B, Destination A (this portal is “B” and you want to go back to “A”)

1 Like

Thank you Brian, that makes a lot more sense. I was able to get it to work by adding a source destination:

    private Portal GetOtherPortal()
    {
        foreach (Portal portal in FindObjectsOfType<Portal>())
        {
            if (portal == this) continue;
            if (portal.source != destination) continue;

            return portal;
        }

        return null;
    }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms