Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Wolfrik_Creations · May 27, 2016 at 02:55 PM · c#javascriptfunctionsstatic-function

Static Function Aliases?

So I think you all know how "transform" is actually "UnityEngine.Transform", and "renderer" was changed to "GetComponent.()" or "GetComponent()".

How can I do this with my static functions? if I have a script named Func with a static function named "MyStaticFunc" then how could I make it where instead of doing "Func.MyStaticFunc(variables in here)" I could just do "myFuncAlias(variables in here)" or something similar.

Thanks in advance! And no need to rush as this isn't an important issue, but more of just a question. :)

Comment
Add comment · Show 16
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 Alec-Slayden · May 27, 2016 at 03:10 PM 2
Share

This isn't a direct answer to using static aliases, but there might be a misconception here. "transform" will return the UnityEngine.Transform that is attached to the game object. It's a property that calls GetComponent() in the getter.

"renderer" used to do the same thing for mesh renderers, but is now obsolete, I believe.

However you can write non-static properties that do something similar and cache the results in a private field. For example, all of my base behaviours have this property:

 private Transform _transform;
 public Transform Transform
 {
     get
     { 
         if (_transform == null) _transform = GetComponent<Transform>();
         return _transform;
     }
 }

Then I can say myObject.Transform and it will only call get component if the component has not yet been found, and it will never return null. This is with c# properties. If you're using Javascript, I'd probably just cache them in awake ins$$anonymous$$d.

avatar image Wolfrik_Creations Alec-Slayden · May 27, 2016 at 05:11 PM 0
Share

Any idea how to convert this to UnityScript? I put C# in the tags because I can translate most C#, though this is new to me but this is EXACTLY what I was looking for.

Thanks for the reply! :)

avatar image Alec-Slayden Wolfrik_Creations · May 28, 2016 at 04:41 AM 0
Share

in unityscript you can declare explicit set and get functions if you inherit monobehaviour, take a look at this post http://answers.unity3d.com/questions/51910/can-i-declare-properties-in-js.html

Show more comments
avatar image Wolfrik_Creations · May 27, 2016 at 09:51 PM 0
Share

$$anonymous$$y email says that ninja_gear posted a comment saying "

 So, what you are saying is you want to reference a GameObject variable and access a $$anonymous$$onoBehavior component attached to that GameObject, and you wish to do so without a GetComponent() call? Similar to the way:

  Transform myTransform = transform;

 retrieves that GameObjects Transform component? 

" but I can not find it, so this is my reply to it.

Yes that is EXACTLY what I was looking to do, so if that's possible then that would be awesome!

avatar image ninja_gear Wolfrik_Creations · May 28, 2016 at 04:04 AM 0
Share

This is an example of the methodology I use to remove all but one GetComponent() call for each component in each Class: (not sure about the Unityscript, I haven't used it in awhile).

 class myComponent : $$anonymous$$onoBehavior
 {
   var xform : Transform;
   var myScript : myCustomClass;
   function Awake()
   {
     xform = transform;
     myScript = GetComponent.(myCustomClass);
   }
 }

References are basically just routing commands, they take up zero resources. The instantiated object is what eats CPU/GPU/RA$$anonymous$$. Since the GameObject is there and running, and the Components are already active and running on it, its best to get a reference to any Components the script is using when those GameObject's are created, that way you don't ask Unity to de-reference the Component during runtime. Then throughout the script, just use the reference.

avatar image Wolfrik_Creations ninja_gear · May 28, 2016 at 04:13 AM 0
Share

Would this work to where I can put that in one static class and then from then on use "gameObject.item" ins$$anonymous$$d of "gameObject.GetComponent(ItemPickUp)"?

It seems like it shouldn't be hard to do as "GetComponent(Transform)" automatically works as "transform".

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · May 27, 2016 at 05:53 PM

Apart from the misconception that other people explained, what you want to do can't be done in Unity right now. There's a new feature in C# 6 that's exactly for that, you write "using static " and the class that has the static methods: http://intellitect.com/static-using-statement-in-c-6-0/

Comment
Add comment · Show 8 · 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 Wolfrik_Creations · May 27, 2016 at 05:56 PM 0
Share

I explained the question poorly, I want to be able to do "someGameObject.structure" ins$$anonymous$$d of "someGameObject.GetComponent(StructureCore)" and "someGameObject.item" ins$$anonymous$$d of "someGameObject.GetComponent(ItemPickUp)".

So this isn't possible? Well if so then that sucks but are there any workarounds?

avatar image Glurth Wolfrik_Creations · May 27, 2016 at 06:32 PM 0
Share

Oh! $$anonymous$$ake a member of your monobehavior called "structure". In Start() call.. structure=GetComponent<StructureCore>(); Done (just don't forget to check for null when you used it). If it might change, take a look at "accessors" (get/set stuff).

avatar image Wolfrik_Creations Glurth · May 27, 2016 at 06:59 PM 0
Share

I know, but they are called thousand of times using different objects. But I'd like to be able to use it just like "someGO.transform.position" so I could do "someGO.structure.structureHP".

I know about creating variables but these are player placed objects, it's not too big of a deal but it would save me a good bit of time ins$$anonymous$$d of putting GetComponent(StructureCore).

avatar image DiegoSLTS Wolfrik_Creations · May 28, 2016 at 01:57 PM 0
Share

Your question was really confusing, the first part where you talk about transform and renderer is done, like everyone said, using properties and it works with both C# and UnityScript: http://unity3d.com/es/learn/tutorials/topics/scripting/properties

You can do that for sure.

The second part where you talked about static functions is what you can't do, you can't access static functions without specifing the class (unless you're calling a static function in the same class it's defined). Note that you're talking about REALLY different things here.

avatar image Wolfrik_Creations DiegoSLTS · May 28, 2016 at 06:27 PM 0
Share

I know that you cannot access static features without the class name, I use static features a lot.

But I was wondering how Unity did it, but I guess it's just hard-coded.

Show more comments
avatar image cjdev · May 28, 2016 at 09:41 AM 0
Share

This looks pretty sweet. using UnityEngine.Debug anyone?

avatar image DiegoSLTS cjdev · May 28, 2016 at 01:59 PM 0
Share

Yes, UnityEngine.Debug and $$anonymous$$athf are great candidates for that new feature.

avatar image
0

Answer by Glurth · May 27, 2016 at 04:50 PM

"UnityEngine.Transform" Refers to the namespace, dot the class. If you put using UnityEngine at the top of your file, you wont need to always specify the namespace and can call the class simply "Transform".

The only place you will ever be able to call a static member of your class, WITHOUT specifying what class you are talking about, is INSIDE the class in question. e.g.

 Class Func
 {
    void Func1()
    {  StaticFunc();// since I'm "inside" the Func class, it assumes I'm talking about a member.
    }
    static void StaticFunc()
    {... }
 }
   
 Class AnotherClass
    {
    static void StaticFunc(){...}// since this class & function MIGHT exist, we NEED to specify the class when calling StaticFunc() from outside of these classes
    }
Comment
Add comment · Show 1 · 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 Wolfrik_Creations · May 27, 2016 at 05:14 PM 0
Share

I know that you do not have to put UnityEngine but I was just saying that it's derived from UnityEngine.Transform.

And you don't have to put UnityEngine while in a class as long as you do this:

 class $$anonymous$$yClassName extends $$anonymous$$onoBehaviour{
 
 }

Also, thank you for the reply!

avatar image
0

Answer by ninja_gear · May 28, 2016 at 07:05 PM

If you want to extend MonoBehavior to include your own custom methods, such as an function that always attempts to fetch said Component, you can, either directly or through inheritance.

     class MyNewGameObject extends MonoBehaviour
     {
         var myCustomClass : CustomClass;
         function Awake()
         {
             myCustomClass = gameObject.GetComponent(CustomClass);
         }
         function GetMyCustomClass()
         {
             return myCustomClass;
         }
     }

You should be able to call this thru .myCustomClass, or .GetMyCustomClass();

*Note: the function is not static as you will be calling it on an instance of the MyNewGameObject.

Comment
Add comment · 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
0

Answer by gorsefan · Jun 09, 2016 at 12:14 PM

You cannot alias functions in C#, the closest you can get is wrapping them in something else or maybe using delegates.

You can alias classes/namespaces/types with the "using" directive.

Also, if you do this kind of thing you end up with code only you will understand. And the you in 6 months time probably won't understand it either ;)

Comment
Add comment · 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

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

166 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

GetComponentInChildren not working in a function, help please? 2 Answers

OnTriggerEnter function in c# 0 Answers

Distribute terrain in zones 3 Answers

Unity2D moving on the y-axis 0 Answers


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