- Home /
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
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.
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)