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 magnusm · Jan 12, 2014 at 06:38 PM · javascriptfunctionclassabstract

Function Abstraction Help

Hi, I am only going to show you snippets of codes, as the full scripts are too long.

The first script is where all the data is stored and has lots of functions to manipulate it. The function in question is like the one below (There are more, but they produce the same error):

 var dataDict = new Dictionary.<int,LifeClass>();
 
 public class InfoClass
 {
     var id : int;
     var object : GameObject;
     var script : Lifeform;
 }
 
 public class LifeClass
 {
     var self = InfoClass();
     var mother = InfoClass();
     var father = InfoClass();
     var partner = InfoClass();
     var children : List.<InfoClass> = new List.<InfoClass>();
 }

 ...

 function GetMother(targetId : int, mode : int)
 {
     if (mode == 0)
         return dataDict[targetId].mother.id;
     else if (mode == 1)
         return dataDict[targetId].mother.object;
     else if (mode == 2)
         return dataDict[targetId].mother.script;
 }

 

The second script that controls the AI, is where the functions are called. I am trying to keep a level of abstraction, to make everything easier for the user (me) to understand:

 d = gameObject.Find("Data").GetComponent(Data);
 
 ...
 
 d.GetMother(id, 1).transform;


Error reads 'transform' is not a member of 'Object'.
It should be a member as its type is GameObject.

What am I overlooking? Or is this just not possible?

Thanks for your time, sorry for the large amount of text.
Magnus :)

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

1 Reply

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

Answer by magnusm · Jan 12, 2014 at 07:02 PM

Dont worry I found the solution courtesy of Eric5h5:

  • Remove #pragma strict at the start

lol :P

Comment
Add comment · Show 4 · 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 semiessessi · Jan 12, 2014 at 07:07 PM 1
Share

so that you understand why this is a fix - pragma strict enforces some degree of type safety. It is precisely because you are returning an 'Object' instance from Get$$anonymous$$other ins$$anonymous$$d of a 'GameObject' instance that this happens. A fix that doesn't lower your program$$anonymous$$g defenses is to return the correct type from the function i.e. use 'function Get$$anonymous$$other( targetID : int, mode : int ) : GameObject' as the prototype - however you will need to seperate out the functions by return type to use this. There is no way currently to know what Get$$anonymous$$other will return at compile time. However you are assu$$anonymous$$g it has the tranform member, so we are not returning the int (or possibly Lifeform).

EDIT: btw i would recommend never program$$anonymous$$g in this way - its not necessary and it makes code so hard to read that /the compiler/ can not understand your intent, let alone a human being - this is why the error occurs at all. every function can have a definite return type - even if you consider OO practices like inheritance - its exceptionally rare to need very late binding in practice, generally a network connection or an extremely hard application boundary has to be involved to really justify it - and even then late binding functionality in most languages is enormously heavyweight and overkill for what is needed...

avatar image magnusm · Jan 12, 2014 at 07:26 PM 0
Share

Sweet, that's really helpful :)

I am going to bring back #pragma strict, and separate out the functions by type :)

Thanks

avatar image Bunny83 · Jan 12, 2014 at 09:01 PM 1
Share

Why using seperate functions? you can just return the mother instance:

 function Get$$anonymous$$other(targetId : int) : InfoClass
 {
     return dataDict[targetId].mother;
 }

So ins$$anonymous$$d of the mode parameter you can pick the desired variable:

 d.Get$$anonymous$$other(id).object.transform;

However if you really need them as seperate methods, feel free to implement them...

avatar image magnusm · Jan 12, 2014 at 10:10 PM 0
Share

Ha, didn't know you could do '.object'. Very cool, thanks :P

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

20 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

Related Questions

Access function (and variable) outside class in the class in Javascript. 1 Answer

Camera Move 1 Answer

How to call a function from a script in another scene 5 Answers

Public Class Error 1 Answer

Custom JS class error - function is not a member of the class 2 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