- Home /
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.
Answer by DarkToadster · Mar 16, 2018 at 12:46 PM
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 :)
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);
}
}
}
Your answer
Follow this Question
Related Questions
How do you REALLY freeze a Rigid Body (no dragging allowed)? 1 Answer
Attempt to disable constraints using RigidbodyConstraints.None has no effect 0 Answers
Freeze position without Rigidbody 1 Answer
Freeze a Rigidbody2D's constraints and move the object manually with translate/Movetowards 1 Answer
How to freeze position x and z on rigidbody(both!) 2 Answers