Player won't stay on moving floor

Hey guys what’s up? I’m trying to make a 2d super mario-like game. It has movable floors that go either up/down or left/right in a loop. What I want to do is to set the player character’s transform.position to be the same as the floor he’s on as long as he’s on one of them so that he moves with the floor but he keeps on falling. Here’s the script:

var player : GameObject;

function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "FloorUpDown"){
transform.position.y = gameObject.transform.position.y;
}
else
if(col.gameObject.tag == "FloorLeftRight"){
transform.position.x = gameObject.transform.position.x;
}
}

function OnCollisionExit(col : Collision){
transform.position.x = player.transform.position.x;
transform.position.y = player.transform.position.y;
}

try this:

var player : GameObject;
 
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "FloorUpDown"){
player.transform.position.y = gameObject.transform.position.y;
}
else
if(col.gameObject.tag == "FloorLeftRight"){
player.transform.position.x = gameObject.transform.position.x;
}
}
 
function OnCollisionExit(col : Collision){
player.transform.position.x = player.transform.position.x;
player.transform.position.y = player.transform.position.y;
}

by the way, where is this script attached to?

i think something is wrong with your code. i can’t tell if this is attached to the floor or to the player.
if this is attached to the floor, try this:

var player : GameObject;
 
function OnCollisionEnter(col : Collision){
if(this.gameObject.tag == "FloorUpDown" && col.gameObject.tag == "Player"){
player.transform.position.y = this.gameObject.transform.position.y;
}
else
if(this.gameObject.tag == "FloorUpDown" && col.gameObject.tag == "Player"){
player.transform.position.x = this.gameObject.transform.position.x;
}
}
 
function OnCollisionExit(col : Collision){
player.transform.position.x = player.transform.position.x;
player.transform.position.y = player.transform.position.y;
}

or try doing it like:

var player : GameObject;
 
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "FloorUpDown")
player.transform.parent = this.gameObject.transform;

}
 
function OnCollisionExit(col : Collision){
player.transform.parent = null;
}

i hope this helps

Ok after hours i finally figured it out. Anyone with the same problem just place this script on the moving floors.

var player : GameObject;
var floor : GameObject;

function OnCollisionEnter(col : Collision){
    if(col.gameObject.tag == "Player"){
    player.transform.parent = floor.transform;
    }
    }

function OnCollisionExit(col : Collision){
player.transform.parent = null;
}

Hope it helps everyone with the same problem & I would like to than Auggie for giving me the idea of using the parent. 10x bro