Variable from Void not working

Long story short I’m trying this:

private Vector2 scrollPosition = Vector2.zero;

private void OnGUI()
{
	scrollPosition = NewScroll(1);

	if (GUI.Button (new Rect (0, 0, 50, 50), "Single Player")) { // First Element
		singlePlayerInit();
	}

	GUI.EndScrollView();
}

private void NewScroll (int noOfElements)
{
	return GUI.BeginScrollView(new Rect(20, 20, 50, 50), scrollPosition, new Rect(0, 0, 50, 50 * noOfElements)); 
}

From the documentation’s GUI.BeginScrollView

Unity is returning

  • A return keyword must not be followed by any expression when method returns void
  • Cannot implicitly convert type 'UnityEngine.Vector2' to 'void'

The whole reason I want to do this is because I’m reusing that tiny bit of code a lot and just want a quick function that generates the height according to how many “elements” there are in my vertical list of elements.

Is there a way of getting around this?

The error messages are telling you that your function NewScroll has a return type of void (which means it returns nothing) but you’re returning a vector2. Just change the void type to vector2 and it should work. You don’t have to do what you wrote as a comment.