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 /
This question was closed Jul 14, 2016 at 04:03 PM by joshua-lyness for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by joshua-lyness · Jul 13, 2016 at 05:28 PM · functionclasscustomtypereturn

Return a custom class from function

I have a variable that is of type class. Within that class is a variable of another class type.


I need to change the values of the class within the class, and I thought I could do that by creating a function that can return the type class. But I can't seem to figure out how.


I know I could do this with a constructor, and pass in the values I need, but it would be so much nicer if you could create a function that returns the class instead.


Can you do this?


Thanks for your help,


EDIT : sorry, yea reading this back it doesn't really make a lot of sense.


so I have a couple of classes, with one of them holding the other class as a variable.

 public class bezier {
         public Vector3[] controlPoint;
         public float length;
         public boundingbox boundary; 
     }
 
     public class road {
         public int typeIndex;
         public int[] intersection;
         public bezier[] spline; 
     }

I then declare a new variable of type "road". I later need to change the information in spline. There's a lot of information in spline, and I was hoping I could do something like this :

 example.spline = SetSplines(some inputs...);

with SetSplines being a function, that returns the data type spline.

 Spline SetSplines (parameters){
 
         ....using the paramaters, add values to the spline class
        return theSplineInfo
 
 }

  • didn't add what Id like to do in the function, its not really relevant*

Now you cant do this, because you cant make the type of the function some custom type. The work around would be to make it a void function, and have it save to some temporary variable of type spline, and then copy the contents of this temporary variable into example.spline . But that's really messy, and I was hoping there was a workaround. :) Thanks

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 wojtask12 · Jul 13, 2016 at 05:59 PM 0
Share

you need to give us some example code - i've got no idea what do you want to achieve

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 13, 2016 at 06:01 PM

edit

Well, so lets start over. Since your "spline" variable is of type bezier[] your SetSplines method has to return an array of bezier, not a single bezier.

 bezier[] SetSplines()
 {
     // create a bezier array which you return at the end
 }

Since you said it doesn't matter what your method does we're done here.

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 joshua-lyness · Jul 13, 2016 at 09:13 PM 0
Share

so within the SomeOtherClass, GetOther() is a constructor right? like a function within the class? Is that how I could do it? Could I write : Example.Spline = GetOther(parameters...);

avatar image Bunny83 joshua-lyness · Jul 14, 2016 at 02:57 AM 0
Share

No, that wasn't a constructor, it was just a method that returns the reference which was stored insode the outer class. However that's pretty irrelevant as your question was misleading and my old example has no relevance here.

I've edited my answer.

avatar image joshua-lyness Bunny83 · Jul 14, 2016 at 04:03 PM 0
Share

yea, it does work. Thanks so much, saved the day xD

avatar image joshua-lyness · Jul 14, 2016 at 01:24 PM 0
Share

$$anonymous$$onodevelop doesnt try to autocorrect it, so i assumed you couldnt do that. Thanks, ill go try it!

Follow this Question

Answers Answers and Comments

48 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

Related Questions

type function not returning. c# 2 Answers

Custom Documentation for Monodevelop 1 Answer

Magic Spell Types - SpellType.Fire/Water/Earth? 1 Answer

Access a class on another script? 1 Answer

How can I add a new "Inspector" 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