C# Learning

Hi,
I started learning C# for Unity using Monodevelopment.
At the moment im following a tutorial and after each chapter I try out what I have learned.

So I tried and got the “not all code paths return value”
I get what the erros message is saying, but dont have a clue how to fix the code.

Here it is:

using UnityEngine;
using System.Collections;

public class Try3 : MonoBehaviour 
{
	public int number1 = 5;
	public int number2 = 10;
	public int number3 = 15;
			
	void Start ()
	{
		int answer =
		Addnumber(number1,number2);
				
		DisplayResult (answer);
	}
	int Addnumber (int num1,int num2)
	{
		int result = num1 + num2;
		return result;
	}
	
	int DisplayResult (int answer)
	{
		Debug.Log ("Your Answer is:" + answer);
	}
}

I still have very little C# knowledge so it will most likely be a very noob error, but getting some interaction from Experts might help my learning process.

Thanks!

This part of the code doesn’t return an int.

int DisplayResult (int answer)
    {
        Debug.Log ("Your Answer is:" + answer);
    }

This is what it should be:

int DisplayResult (int answer)
    {
        Debug.Log ("Your Answer is:" + answer);
        return answer;
    }

But I don’t know why you would want to return your input? :face_with_spiral_eyes:

Make the return type of your DisplayResult method (the int at the start) void instead of int. That way it won’t expect to return anything.

AH YES!
Thx christinanorwood!

change int DisplayResult (int answer) to void DisplayResult (int answer)

Your version is a function which is expecting you to return a value.

Edit: djfunkey and christianorwood beat me to it :stuck_out_tongue:

Dont worry, there will be a lot more questions incoming.:wink:
Thanks anyway!