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 dhore · Dec 11, 2015 at 12:36 PM · transformclassaccesssecuritystruct

How to get Scripts attached GameObject from within a Struct declared in the Class

Example layout:

 public class SomeClass : MonoBehaviour {
 
     private struct SomeData
     {
         private Transform obj;
 
         SomeData(Transform t)
         {
             if (t.parent == transform) <------ Cannot access "transform"
                 obj = t;
             else
                 // throw an error
         }
     }
 }

I want to access the Transform that this Script is attached to as I need to make sure that the Transform that's passed into the Struct's constructor is a Child of the Transform that the Script is attached to (hope that makes sense).

Note: I've tried adding a public Property to the Class, but the Struct still has no access to it:

 // within the Class specified above
 public Transform GetTransform { get { return transform; } }

Any help is very much appreciated.

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

Answer by fafase · Dec 11, 2015 at 01:06 PM

I won't discuss the design at first but will only fix the issue. The struct is no MonoBehaviour so it does not contain the transform reference, you need to pass it as well.

 public class SomeClass : MonoBehaviour {
  
      private struct SomeData
      {
          private Transform obj;
  
          SomeData(Transform transform, Transform t)<------ Pass "transform"
          {
              if (t.parent == transform)
                  obj = t;
              else
                  // throw an error
          }
      }
  }

Now about the design, it would easier to make the check out of the constructor so you only create the struct if the check is ok.

Comment
Add comment · Show 1 · 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 dhore · Dec 11, 2015 at 03:58 PM 0
Share

I was trying to not have to pass it as a variable, but I guess if that's the only way then I'l do it like that. And since the struct's private to the class I guess it's not a big deal if it's a parameter anyway..

Thanks for the response.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Finding class where current GameObject is assigned to as a var 1 Answer

Best way to calculate sum of custom object holding numbers and returning sum as that object instance 0 Answers

Is it possible to Change mesh also multi materials by a button or a key 0 Answers

Accessing another script from a class 3 Answers

How would I access a Class Variable from another script? 1 Answer


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