Unfreeze constraint on enter,freeze on start

id like to make it so when I start my game it finds objects with a specific tag and uses rigid body’s constraints to freeze all rotation and positions, but when I enter an object (so with collider) it finds those specific tagged items and unfreezes those constraints. I have tried quite a few scripts but none seem to work the way I wish, if you have a script please share it with me. Thank you.

Hello @coltanjamesshaw02.


You can freeze all constraints of objects with a specific tag using this code:

private void Awake()
    {
        GameObject[] objectsToFreeze = GameObject.FindGameObjectsWithTag(OBJECT_TO_FREEZE_TAG);

        for (int i = 0; i < objectsToFreeze.Length; i++)
        {
            Rigidbody rigidbody = objectsToFreeze*.GetComponent<Rigidbody>();*

rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
}
----------
One of the ways to unfreeze objects constraints on collision enter is to add a separate script to such object that will turn them off after colliding with Player:
using UnityEngine;
using UnityEngine.Assertions;

public class ObjectToFreeze : MonoBehaviour
{
private const string PLAYER_TAG = “Player”;

[SerializeField] private Rigidbody _rigidbody;

#region MonoBehaviour

private void Awake()
{
Assert.IsNotNull(_rigidbody);
}

private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag(PLAYER_TAG))
{
_rigidbody.constraints = RigidbodyConstraints.None;
}
}

#endregion
}
All the best,
The Knights of Unity