Answers for "Get Set Return method, properties stuff?"
http://answers.unity.com/questions/1495277/get-set-return-method-properties-stuff.html
The latest answers for the question "Get Set Return method, properties stuff?"Answer by djordr
http://answers.unity.com/answers/1495631/view.html
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.Wed, 18 Apr 2018 14:21:40 GMTdjordrAnswer by Hellium
http://answers.unity.com/answers/1495334/view.html
I don't really understand what you don't understand...
<br>
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;
}
}Tue, 17 Apr 2018 21:54:16 GMTHellium