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;
         }
    
    }

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 :slight_smile:

[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).