How to freeze the position of an object when a canvas gets activated?

How do I set the Y-position of an object to freeze when a canvas gets activated? What function can I use in my code? Sorry if this question is stupid, I am a complete beginner.

Hey jo.
The best solution would be if you look for some Beginner C# and general Unity tutorials.
Brackeys is a good Youtuber. You should check him out.

But i’ll help you. The code is really self explaining :slight_smile:

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

public class IsCanvasEnabled : MonoBehaviour {

public GameObject canvas;
public Transform objectToFreeze;

public float y;

// Update is called once per frame
void Update () {

    // If the Canvas Object in Scene is active ...
    if (canvas.activeInHierarchy)
    {
        // Set position of objectToFreeze
        objectToFreeze.position = new Vector3(objectToFreeze.position.x, y, objectToFreeze.position.z);

        // Use this instead if you want to freeze the object wich has this script
        transform.position = new Vector3(transform.position.x, y, transform.position.z);
    }
}

}