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 soulburner · Apr 17, 2012 at 11:49 PM · javascriptclassreferencereflection

UnityScript - Class reference

My question is how do I use a link/reference/reflection for my class?

Here goes my ActionScript code that I want to realize in UnityScript. Let's assume I have a public class named "MyClass" and I want to give it as a parameter to my function.

 function CreateObject(user_class: Class): Object {

     var o = new user_class();
     return o;

 }
 ...
 var my_obj = CreateObject(MyClass);

How can I make it in UnityScript?

PS: Of course, I don't need such simple function. But it shows an idea.

Comment
Add comment · Show 2
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 syclamoth · Apr 17, 2012 at 11:50 PM 0
Share

I could show you how to do that in C#. Do you want me to? Unfortunately, I'm not too familiar with Unity's weird Javascript implementation...

Ok, I just wrote an answer for this, and I realised that there is very little practical difference between this and just using a plain constructor. Why can't you just do that?

avatar image soulburner · Apr 18, 2012 at 12:22 AM 0
Share

I know how to do that in C#, Java and ActionScript. But now I wounder how to do that in JS/UnityScript. The idea was not just about creating an instance. The idea was to make some classes (inherited from the base interface) with the fixed set of methods. And to make a class that can work with any of these classes.

As I've said in the comment below, the first idea was to create an instance of my class and to use an instance as a parameter, but that seems to be a bit not right for me - when you're creating an instance just to use its (static) methods, without any need of instance itself.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Apr 17, 2012 at 11:59 PM

Well, if you want it, here's some C# code:

 public T CreateObject<T>()
 {
     return new T();
 }

 var my_obj = CreateObject<MyClass>();

Of course, why can't you just use

 MyClass my_obj = new MyClass();

?

The only functions you would have available in 'CreateObject' would be ones that are common to all object types- pretty much just ToString, GetHash, and a few other things. If you wanted to use this for something useful, I would recommend creating an interface that defines the commonly-used functions for those classes that you would like to use in this way. Then, limit the function so that it only accepts classes that implement that interface.

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 soulburner · Apr 18, 2012 at 12:14 AM 0
Share

$$anonymous$$y code above is just an illustration. Of couse, the real code is $$anonymous$$UCH more complex then just creating an instance.

Ok, your code on C# is good, but that doesn't help me - I need to implement something like this in UnityScript ;)

About the 2nd part of your answer - yeah, that was my first idea! But that's looks like a little not-right. Creating an instance of a class just to call some methods - that is not what the objects in OOP are for :) So, I thought if there is another (more OOP-correct) way to do something like that.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a dynamic array of objects of custom class 1 Answer

Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer

Referencing subclass in Unity JS 2 Answers

How can I declare class properties of an object in a single line? 2 Answers

Create an array of classes, of different type. 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