Falling object,add rigidbody

I want to make an object fall down when it collides with something. I’m totally new to unity. My code isn’t working!

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class script : MonoBehaviour
{
    public GameObject Floor;
    void OnCollisionEnter(Collider other)
    {
        Floor.AddComponent<Rigidbody>();
    }
}

,I’m trying to make a cube that when something collides with it, it just falls down. I already made a code but it doesn’t seem to work. I’m really new to Unity and I’m trying to impress my teacher with a game for an optional project.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class script : MonoBehaviour
{
    public GameObject Floor;
    void OnCollisionEnter(Collider other)
    {
        Floor.AddComponent<Rigidbody>();
    }
}

Hey, don’t worry we all begin somewhere.
At first: don’t add the Rigidbody via script at runtime.
That won’t work because of the way Unity detects collision.
It’s not the collider that detects it, it’s the rigidbody.

You can attach it to your cube and disable it.
Here is a simple script you can attach to your cube(AFTER YOU ADDED THE RIGIDBODY COMPONENT)

public class FallOnCollision : MonoBehaviour
{
    Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true; // This freezes the cubes physics, so it only detects collision
    }

    public void OnCollisionEnter(Collision other)
    {
        if (rb) // Checks if there is a Rigidbody referenced
            rb.isKinematic = false; // Reactivates the physics of the cube
    }
}

It activates the physics as soon as another collider with a rigidbody attached collides with it.

Hope that worked out and good luck with your project.

Greetings,

Max