0
\$\begingroup\$

So I've been working on a little project that involves Valve's "Portal" style portals, the type where you can seamlessly see what's on the other side and walk through. I've run into a problem when it comes to teleporting an object to it's new location upon hitting the portal. I have a List<GameObject> that contains all the objects that are in close enough range to feasibly travel through the portal, but when I try to modify the transform.position of one of these GameObjects, nothing happens, no error messages.

So I've been going through my script line by line and writing little dummy functions to test different parts of it to see where the problem lies, and everything seems to work up until I index the list and try to modify a value, nothing happens. So was thinking that perhaps the List doesn't actually contain a reference to the GameObject in the scene, but simply a copy of the GameObject's data at the time it was passed into the list. So I checked this by making the list public so that I see it in the inspector, and when I click on an item in the list it highlights that same GameObject in the hierarchy, which suggests it is actually a reference. See bellow...

enter image description here

I am at a loss as to what's happening here and would really appreciate if anyone could offer a suggestion, Here is the dummy function where I try to arbitrarily modify the GameObject data from the List...

        if (Input.GetKeyDown("k"))
        {
            for (int i = 0; i < trackedTravelersObjects.Count; i++)
            {
                trackedTravelersObjects[i].transform.position += new Vector3(0, 30, 0);
                Debug.Log("K");
            }
        }

You'll notice a Debug.Log("k"), this is just to check that the code is actually being called, which it is.

Thanks.

\$\endgroup\$
1
  • \$\begingroup\$ Ah yes sorry I didn't see that, the player has a character controller on it that uses the CharacterController.Move() method to move around based on keyboard input. \$\endgroup\$
    – DBurn
    Nov 10 '20 at 13:54
0
\$\begingroup\$

Your problem is not the list, it's the CharacterController component.

(Testing this code inside and outside of the list, or with different objects would let you isolate this)

The character controller caches the last position it moved to, and performs its next move from there - no matter where the transform has travelled in between.

Using the advice in this thread, we can adapt your code to...

if (trackedTravelersObjects[i].TryGetComponent(out CharacterController character)
&& character.enabled) {
    character.enabled = false; // Put the character to sleep for a moment.
    trackedTravelersObjects[i].position += new Vector3(0, 30, 0);
    character.enabled = true; // "Wake up" the character in its new place.
} else {
    trackedTravelersObjects[i].position += new Vector3(0, 30, 0);
}
\$\endgroup\$
1
  • \$\begingroup\$ Worked perfectly, can't believe it was something so simple, I've been stuck on this for 2 days. Thanks for your help \$\endgroup\$
    – DBurn
    Nov 10 '20 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.