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 maxxa05 · Aug 11, 2012 at 08:11 AM · gameobjecttransformclass

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;
          }
     
     }
Comment
Add comment · Show 1
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 maxxa05 · Aug 10, 2012 at 10:23 PM 0
Share

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;

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · 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 maxxa05 · Aug 11, 2012 at 07:50 PM 0
Share

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.

http://wiki.unity3d.com/index.php/A$$anonymous$$anagerClass

avatar image DESTRUKTORR · Aug 12, 2012 at 01:06 PM 0
Share

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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