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?
I'm thinking something like this?
private Vector2 scrollPosition = Vector2.zero;
private void OnGUI()
{
scrollPosition = GUI.BeginScrollView(NewScroll(1));
if (GUI.Button (new Rect (0, 1 * (buttonY + paddingY), buttonX, buttonY), "Single Player")) { // First Element
singlePlayerInit();
}
GUI.EndScrollView();
}
private void NewScroll (int noOfElements)
{
return (new Rect(20, 20, 50, 50), scrollPosition, new Rect(0, 0, 50, 50 * noOfElements)));
}
But how do I return in the correct format that ´BeginScrollView()´ wants?
Answer by DiegoSLTS · Aug 08, 2016 at 12:03 PM
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.