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 /
  • Help Room /
avatar image
0
Question by djordr · Apr 17, 2018 at 07:31 PM · methodpropertiesreturnset

Get Set Return method, properties stuff?

What exactly is the point of this?

 public int HelloThere(bool talking)
 {
 Return;
 }

I understand variables just fine, how to change them, why you want to change them. But putting an int into a method or having a method return a bool. I do not understand why you would want to do it, why it's done and no matter how many videos I've watched in which people with a lisp are explaining it I still don't understand it. We want it to return a bool, return a bool huh mr smartypants to whom your mom?

It seems like every person trying to explain this assumes people understand what they mean by returning a bool. Now that we've decided to have the method receive a string and return an int we're going to set and get the method. Type set; then type get;

It's so mindbogglingly retarded. Can someone explain this in a non autistic way or perhaps paste a link to some practical example of this and how exactly you use it and what's the point of it. It's beyond annoying watching hours of tutorials on people trying to explain this but because they're so bad at explaining it just end up wasting my time. And if you decide to close this question because you think it's obvious then shame on snorty.

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

2 Replies

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

Answer by djordr · Apr 18, 2018 at 02:21 PM

I'll explain it in non autistic terms my friend. In the Start function below we want the answer, who wouldn't want the answer?! So we create an int called answer.

 void Start ()
 {
         int answer;
 }

What do we want the answer to? We want an answer to some math problem. So we create this function:

 int MathProblem(int apples, int oranges)
     {
     }

We want the answer in our start function to be equal to how many apples and oranges there are and we want the math to take place in the MathProblem function. The int at the start of the function means that we want the return value of our function to be an int. I'll explain what I mean by the return value to be an int in just a second. The two ints we created called apples and oranges represent values we pass into the function. To pass values into the function we write in our start function this.

 void Start ()
  {
          MathProblem(5,4);
          int answer;
  }

What this means is that the two ints we created in the MathProblem function called apples and oranges. Their value is going to be equal to 5 and 4. So there are going to be 5 apples and 4 oranges. Now let's figure out how many apples and oranges we've got.

     int MathProblem(int apples, int oranges)
          {
 int fruit;
 fruit = apples + oranges;
 return fruit;
          }

This is self explanatory, apples+oranges. The two values we passed into our function is going to be equal to a newly created int called fruit. Now we type return fruit; This is going to make the answer to our math problem equal to the int we want the function to return. The int answer; we created in start function we now want to make equal to our mathproblem function. So in Start we type:

 void Start ()
   {
           int answer;
           answer = MathProblem(5,4);
   }

So the answer is going to be equal to 9. Yes you could just type answer = 5+4. But that math function could be a lot more complicated and you could pass in different values which would make the function return different answers. It works a lot like a calculator. I have yet to understand getters and setters because I have yet to come across someone who explains it in non autistic terms. So sorry this is all I got for now, hope you understand the purpose of passing variables into a function and returning something a bit better after my explanation.

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
1

Answer by Hellium · Apr 17, 2018 at 09:54 PM

I don't really understand what you don't understand...


Take the very simple function:

  public bool AreYouAnAdult( int age )
  {
      if( age > 18 ) // Or 21 if you come from the USA
          return true ;
      else
          return false;
  }

This function takes an integer, which is your age, and tells you if you are an adult or not.

Then, in your game, supposing you have an input field where the user can type his/her age, the game will be able to indicate whether he/she is an adult or not by calling this function.

 if( AreYouAnAdult( int.Parse( inputfield.Text ) ) // int.Parse will try to convert the string to an integer so that your function can be called
     Debug.Log("You are an adult!");
 else
     Debug.LogWarning("You are not an adult (yet)");


The getters and setters (in any Object Oriented Language) are functions designed to set and get the values of instance's fields with some additionnal constraints (to keep the instance consistent).

 private float playerHealth ;

 // Create a getter in order to display the health of your player on the screen for example
 public float GetPlayerHealth()
 {
     return playerHealth ;
 }
 // Create a setter to alter the health (if your player has been healed or hurt),
 // but make sure the health is correct!
 public void SetPlayerHealth( float newHealth )
 {
      if( newHealth >=0 && newHealth <= 100 )
         playerHealth = newHealth ;
 }


Properties (in C#) are just a nice syntax to write the getters and setters, but you can use simple functions has described before.

 private float playerHealth ;

 // Create a getter in order to display the health of your player on the screen for example
 public float PlayerHealth
 {
     get // Nice syntax for the getter
     {
          return playerHealth ;
     }
     set  // Nice syntax for the setter. Here, the name of the parameter is always `value`
     {
        if( value >=0 && value <= 100 )
            playerHealth = value;
     }
 }
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

133 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

Related Questions

Is there a cleaner way to set all the properties when declaring a new object? 2 Answers

Getter and Setter Question! 1 Answer

Destroy on load/Properties Issue 2 Answers

method return type is incompatible 0 Answers

Trying to import FBX Custom user string properties. Not Showing up? 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