Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 JFGames · Jan 16, 2017 at 01:45 PM · error messagerigidbody2dpuzzleconstraints

Member cannot be accessed with an instance reference; qualify it with a type name instead

I was in the middle of programming a block puzzle, when the block entered a trigger it's position was set to the triggers position and a door opened. So far everything worked, but I wanted to lock the block's position to the trigger so I tried to freeze the Rigidbody constraints but I got an error that said:

"Member 'RigidbodyConstraints2D.FreezePositionX' cannot be accessed with an instance reference; qualify it with a type name instead"

Here's the code:

  void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Box")
         {
             other.transform.position = transform.position;
             Door.SetActive(false);
             other.GetComponent<RigidbodyConstraints2D>().FreezePositionX = true;
 
         }
     }

 



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
2
Best Answer

Answer by doublemax · Jan 16, 2017 at 01:51 PM

 other.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
Comment
Add comment · Show 6 · 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
avatar image MelvMay ♦♦ · Jan 16, 2017 at 02:14 PM 2
Share

Also note that:

 other.GetComponent<Rigidbody2D>()

might return null so you should probably check that too unless you know it'll be there beforehand.

avatar image doublemax MelvMay ♦♦ · Jan 16, 2017 at 02:16 PM 1
Share

Yes. Better safe than sorry :)

avatar image JFGames · Jan 17, 2017 at 03:47 AM 0
Share

Okay, now I've run into a new problem,When I freeze the X axis it removes the constraint I have on the rotation, and I can't have it freeze both X and Y. Sorry to keep bothering you.

avatar image MelvMay ♦♦ JFGames · Jan 17, 2017 at 06:35 AM 1
Share

Use: RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY ... or ... FreezePosition ... which is the same i.e. the two merged together with the | (or operator).

avatar image JFGames MelvMay ♦♦ · Jan 17, 2017 at 05:46 PM 0
Share

Thank you! Now it's working exactly how I wanted it to!

avatar image Bunny83 · Jan 17, 2017 at 07:28 PM 0
Share

Everything correct. Just to add a few words of explanation:

  • RigidbodyConstraints2D is not a class or component but an enum type. The members of an enum are not variables but constants. The Unity documentation frequently labels thing wrong because most of their docs are auto-generated.

  • An enum as type is basically the same as the type "int". So it simply holds a 32bit "number". It just provides some special restrictions to the user so you can only assign values that are defined in the enum(eration) as constants.

  • The "constraints" variable (which is of type RigidbodyConstraints2D) is used as a bitmask.

The enum is declared like this:

 public enum RigidbodyConstraints2D
 {
     None = 0,
     FreezePositionX = 1, // 0000 0001
     FreezePositionY = 2, // 0000 0010
     FreezePosition = 3,  // 0000 0011
     FreezeRotation = 4,  // 0000 0100
     FreezeAll = 7        // 0000 0111
 }

As you can see "FreezePosition" has a value of "3" which is just the combined bit patterns of FreezePositionX and FreezePositionY. Just like FreezeAll has a value of 7 which is FreezePositionX, FreezePositionY and FreezeRotation together

So as example if you want to freeze the "Y" axis and freeze the rotation you would have:

    FreezePositionY    0010     2
 or FreezeRotation     0100     4
 ----------------------------------
  =                    0110     6


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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

No problem on playmode, but shown 3 confusing error when compiled. How to fix them? 0 Answers

RigidbodyConstraints2D overwrites previous RigidbodyConstraints2D 0 Answers

Trying to freeze multiple gameObjects at the same time 1 Answer

Freeze gameobject with a collision 1 Answer

Multi-parent constraint not working - System.InvalidOperationException: The TransformStreamHandle is invalid. Please use proper function to create the handle. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges