'Teleporting' to loop a map

I’ve searched through the various scripting references, but haven’t managed to recognize how to do what I want:
I’m trying to make objects loop across my 2D map until they’re destroyed by the player.
I have a Boundary with a box collider. When an object exits -
void OnTriggerExit (Collider other)

  • I want to grab other’s Transform position, and if x or z is greater than 20 or less than -20 I want to multiply that value by -1, teleporting it to the other side of the map. My assumption is that since it’s not changing velocity but exclusively position, the object will then keep rolling along, back onto the map for another crossing.
    Right now it’s giving me an error type CS1612: Cannot modify a value type return value of ‘UnityEngine.Transform.position’. Consider storing the value in a temporary variable.
    Apparently I’m unclear how to either initialize or correctly attach a temporary Vector3 for this purpose, because the system has rejected my attempts as invalid.

Hello,

you cannot modify the position vector directly.
As Unity said, you need to store it in a temporary variable to modify it.
I don’t quite understand your code (are you trying to assign a collider to a Vector3?), but maybe try this:

using UnityEngine; using System.Collections;

public class TeleportSquare : MonoBehaviour {

	void OnTriggerExit(Collider other) {
		GameObject otherObject = other.gameObject;
		// Teleport everything that leaves the trigger to the opposite side
		if (Mathf.Abs(otherObject.transform.position.x) >= 20) {
			Vector3 temporaryVector = otherObject.transform.position;
			temporaryVector.x *= -1;
			otherObject.transform.position = temporaryVector;
		}
		if (Mathf.Abs(otherObject.transform.position.z) >= 20) {
			Vector3 temporaryVector = otherObject.transform.position;
			temporaryVector.z *= -1;
			otherObject.transform.position = temporaryVector;
		}
	}
}