• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jo000408 · Mar 15, 2018 at 05:49 PM · freezeposition

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I query the RigidbodyConstraints? 3 Answers

Unstable Rigidbody with freeze position 1 Answer

Stop player from moving with WASD while using mouse still. 2 Answers

I cannot freeze both position on one axis together with rotation 1 Answer

Disabling rigidbody constraints in code - Freeze Position, Rotation etc 7 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges