need help to understand why i get errors on these 2 lines.

hi, i need help i don’t understand why these 2 lines get errors

private void FindClassDescription(int classSelection){  //i get errors from this
        if (classSelection == 0) {
            BaseCharacterClass tempClass = new BaseMageClass();
            return tempClass.CharacterClassDescription;
        }

    }

and GUI.Label(new Rect(450, 50, 300, 300), FindClassDescription(classSelection)); //i get errors from this

this is the entire code:

using UnityEngine;
using System.Collections;

public class DisplayCreatePlayerFunctions {

    private int classSelection;
    private string[] classSelectionNames = new string[]{"Mage", "Warrior", "Rogue", "Cleric"};


    public void DisplayClassSelections(){
        // A list of Toggle Buttons and each button will be a different class
        //selection grid
        classSelection = GUI.SelectionGrid (new Rect (50,50,250,300), classSelection, classSelectionNames, 2);

        GUI.Label(new Rect(450, 50, 300, 300), FindClassDescription(classSelection)); //i get errors from this
    }

    private void FindClassDescription(int classSelection){  //i get errors from this
        if (classSelection == 0) {
            BaseCharacterClass tempClass = new BaseMageClass();
            return tempClass.CharacterClassDescription;
        }

    }

    public void DisplayStatAllocation(){
        // A list of stats with plus and minus buttons to add stats
        //logic make sure player cannot add more stats than given
    }

    public void DisplayFinalSetup(){
        //name
        //gender
        //add a description to your character, a short bio
    }

    public void DisplayMainItems(){
        GUI.Label (new Rect (Screen.width / 2, 20, 250, 250), "CREATE NEW PLAYER");
    }

}

if you’re going to post a thread about “errors” you are getting, please actually include the error text…

from the looks of it you should be getting something complaining about your function having a return type of “void” whilst it’s trying to return some value…

1 Like

unless you’re particularly attached to the legacy UI “ongui” approach I’d suggest moving over to the canvas system it’s simpler to build the more complex user interfaces and more performant

1 Like

oops forgot to put in the errors ._.

here are the errors i get:

error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string)’ has some invalid arguments

error CS1503: Argument #2' cannot convert void’ expression to type `string’

error CS0127: `DisplayCreatePlayerFunctions.FindClassDescription(int)': A return keyword must not be followed by any expression when method returns void

error CS0029: Cannot implicitly convert type string' to void’

Your method is a void method - which means that it doesn’t return anything. You’re trying to return something.

The other error is that you’re trying to show a label with the return value of FindClassDescription, when that method returns nothing (void).

It looks like what you want is for FindClassDescription to return a string - so change the method to have the return type string rather than void.

1 Like

hi, changing it to private string took away most of the errors but gave me a new one : error CS0161: `DisplayCreatePlayerFunctions.FindClassDescription(int)': not all code paths return a value

Yeah. If your if-check fails (ie. classSelection isn’t 0), then you’re not returning something.

Any method that has a return type must return something in all the ways it’s executed. You’ll have to figure out what FindClassDescription should return for different values of classSelection - probably the description for that class.

(a method can also throw an exception instead of returning something - which is useful if you want your code to blow up instead of silently failing if there’s an unexpected input.)

2 Likes

hi, sry im a bit slow at understanding , could you give me an example pls so i get an idea of how to do it?

You can always cheat and return null at the end of your code.

private string FindClassDescription(int classSelection){  //i get errors from this
        if (classSelection == 0) {
            BaseCharacterClass tempClass = new BaseMageClass();
            return tempClass.CharacterClassDescription;
        }
        return null;
    }

i’ve tried that but it keeps giving me an error if i try to “cheat it” ._.

edit: nvm it allows it now for some reason o.o (unity you annoy me sometimes…)

Never mention an error on this forum again without a code, description and line number.

In the nicest possible way. :slight_smile:

having looked at what the code is trying to achieve I think you might be heading down a rather inflexible path.

At the moment when you call “FindClassDescription” you are going to need to check the parameter and hardcode the response (I’d recommend going over to a switch rather than if’s since you’ll need one for cleric, warrior, mage etc.)

What you really want to be doing is storing the information about the character classes in objects and retrieving the information from the “current object” dynamically. Something like scriptableObjects can be used to store the “data” about the character classes for you to access the data without having to instantiate an actual object (which would be done when the user selects that character class). A quick overview on scriptableObjects was covering in the live training a little while back.

1 Like

:slight_smile: i’ll try to keep that in mind

1 Like