Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 VladN · Jan 13, 2012 at 04:17 AM · gameobjecttransformclassfindaccess

Finding class where current GameObject is assigned to as a var

Hi gang,

Working with many class instances, my question:

Can a GameObject whose transform belongs to a class find that class in a simple "transform.myClass = what" kind of way?

i.e. class abcTest {var testObj : Transform;} with a script on the testObj assignment figuring out the exact instance of class abcTest it's testObj transform belongs to.

Looking through an array works, but is rather cumbersome.

My current workaround is upon instantiating a prefab to testObj's slot to pass a variable which stores the class it belongs to so it can access it form it's own script, but was wondering if there is a better way.

Much appreciated!

'V

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 syclamoth · Jan 13, 2012 at 04:26 AM

I don't quite understand your question. An object can have references from several different places- any given object has no way of knowing what other objects are referring to it. For example, you could have two classes-

 class Foo 
 {
     var someTrans : Transform;
 }

 class Bar
 {
     var otherTrans : Transform;
 }

Then, in your final class

 class FooBar
 {
     var bluh : Foo;
     var hulb : Bar;
 }

if you go

 bluh.someTrans = transform;

and

 hulb.otherTrans = bluh.someTrans;

both of those objects have transform variables that refer to the same object!

So, as you can see there is no logical way for an object to definitively know what objects it is a member of, because a single object can be referred to from several different places.

Comment
Add comment · Show 3 · 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 VladN · Jan 13, 2012 at 11:03 PM 0
Share

Thanks syclamoth! I only reference it once, but what you are saying makes sense. So passing manually the class is probably the best option?

class Foo { var someTrans : Transform; }

function NewClass(){

var newFoo : Foo = new Foo();

newFoo.someTrans.GetComponent("SomeScript").someVar = newFoo;

}

(sorry about the code formatting)

avatar image syclamoth · Jan 14, 2012 at 12:33 AM 0
Share

Yes, that's right.

avatar image VladN · Jan 14, 2012 at 01:36 AM 0
Share

Thanks much!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Accessing gameObject or transform from inside a class 1 Answer

Finding object with transform 1 Answer

Find Transform in the scene 2 Answers

Why does this wont work? transfor.find() 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