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 Maffew · Feb 28, 2014 at 10:20 AM · c#class

Return a value from a custom class

Hi all,

I'm fairly new to programming and have been dabbling with C# I'm trying to create a class that returns a value, for example

     using UnityEngine;
     using System.Collections;
     
     public class addValues : MonoBehaviour 
     {
         int val1;
         int val2;
     
         public addValues (int val1, int val2)
         {
                 this.val1 = val1;
                 this.val2 = val2;
         
                 int answer = val1 + val2;
         
                 return answer;
         }
     }

but I get the error "A return keyword must not be followed by any expression when method returns void" Is the default return type void? How would I go about allowing it to return the value?

Thanks in advance

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

3 Replies

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

Answer by Jamora · Feb 28, 2014 at 10:29 AM

Classes cannot return values. Classes are a container for data and functionality to modify that data. What you're trying to do here is create a new object from this class. Creating objects is accomplished using a constructor, which is what you're trying to do here.

Usually classes are not made around functionality (such as adding), instead you would name your class something like NumberPair, which then contains two numbers and has methods which will return the result of operations on those numbers.

      using UnityEngine;
     using System.Collections;
      
     public class addValues : MonoBehaviour
     {
     int val1;
     int val2;
      
     public addValues (int val1, int val2)
     {
     this.val1 = val1;
     this.val2 = val2;
      
     }
 
     public int Add(){
         return val1+val2;
     }
     }

Now you would use use this Add function by first creating a new object of type addValues (really should rename this -- Class names in C# are, by convention, Capitalized) with the following line of code:

 addValues addition = new addValues(3,6); //create object
 
 int result = addition.Add(); //perform the addition
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 Maffew · Feb 28, 2014 at 10:45 AM 0
Share

Ah, that makes so much more sense! Brilliant, Thanks guys!

avatar image Maffew · Feb 28, 2014 at 08:51 PM 0
Share

Ah, that makes so much more sense! Brilliant, thanks guys!

avatar image
0

Answer by moghes · Feb 28, 2014 at 10:26 AM

Dude, your function type must be of the same type of your returning value.

like down there, you are returning answer which is of type "int", so have your function of type int. check the code edited.

 public class addValues : MonoBehaviour 
     {
         int val1;
         int val2;
  
         public int addValues (int val1, int val2)
         {
              this.val1 = val1;
              this.val2 = val2;
  
              int answer = val1 + val2;
  
              return answer;
         }
     }
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 zedsmith52 · May 27, 2018 at 01:43 AM

It seems you have a little confusion between classes and functions. Both answers above are valid and correct, just remember to take some care not to get the two mixed up.

Simply, a class is used to create and object more complex (generally) than native types, such as int, float, etc.

Functions are generally used to perform operations.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Will calling and instance of classA, from classA, repeatedly, cause a stack overflow? 1 Answer

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Field is never assigned to, and will always have its default value 3 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