- Home /
Accessing gameObject or transform from inside a class
I tried to make a singleton class that would contain some information about the spaceship I control in my game. I tried the following code, but I get a compilation error. It seems that I can't reference my gameObject nor my transform of the object I put this script on. Is there any way to reference those inside a custom class inside a script?
FYI, this class is part of a larger script
class SSTransform
{
var sTransform : Transform;
private static var ssTransform : SSTransform;
public static function GetTransform() : SSTransform
{
if (ssTransform == null)
ssTransform = new SSTransform();
return ssTransform;
}
private function SSTransform()
{
sTransform=gameObject.transform;
}
}
I succeeded using the following script, but I would still like to know if there's a way to do it inside another script
SSTransform.js:
pragma strict
var sTransform : Transform; private static var ssTransform : SSTransform;
function Awake(){ sTransform=gameObject.transform; } public static function GetTransform() : SSTransform { if (ssTransform == null)
ssTransform = FindObjectOfType(typeof (SSTransform)) as SSTransform;
return ssTransform;
}
Answer by DESTRUKTORR · Aug 11, 2012 at 01:22 PM
You shouldn't need to go through gameObject
. All you need to do is use transform
(within the same GameObject, that is...).
To do it from another script, you would need to find the GameObject
it belongs to, first.
To do this, you can use
var somethingElsesTransform : Transform = GameObject.Find("Name of the GameObject you're trying to find").transform;
If you do not know the name of the GameObject
you're trying to find, you can also find them in other ways, such as
var goArray : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
var transformToGet : Transform;
for (var go : GameObject in goArray) {
if(go.GetComponent("scriptOrComponentName")!=null)
{
transformToGet = go.GetComponent("scriptOrComponentName");
}
}
Or you could use tags, or layers, or direct-collision detection, or trigger collision detection, and the list goes on. Point is, once you've got the GameObject
reference, all you've gotta do is take that and add a .transform
, and you've got yourself the transform you were lookin' for :)
[edit] I noticed you were trying to use static methods and such... I should note to you that while it may seem convenient to use static methods, as you don't need an object reference, DO NOT USE THEM if you don't have a particular -reason- to use them, apart from that. The reason static methods and fields don't need an object reference is because they change -universally- across the entire runtime element (that is to say, if I had sixty enemies, all from the same class, and their health was a static field, if I lowered the health of one of them, it would lower the health of -all- of them).
Yes, I know it's tricky to use them for multiple instances, but in my case I'm creating a singleton, which by definition only has one instance.
Ahhh, well if I'd known you were using a singleton I wouldn't have said that, haha! But yeah, this is how you would access (most easily) non-singleton (or unknown) game objects. Enjoy ^_^
Your answer
Follow this Question
Related Questions
Finding class where current GameObject is assigned to as a var 1 Answer
GameObject not working with variable in C# script 3 Answers
change the front point or head of a GameObject 1 Answer
Need workaround for this line of code 1 Answer
Instantiate Terrain Object as child of Empty Game Object 1 Answer