Reputation: 1157
I'm making something like an objective list with C#.
I got it working and all, able to draw the objective list inside Unity, and make new objectives with them. I had an idea on how to delete something, I used System.Linq
and the List.First function to search for it. But it does not seem to work.
This is the 'Finishing and Creating Objectives' code.
public void NewObjective(string objectiveText, string objName)
{
objectives.Add(new Objective(objectiveText, objName)); //Add to the list Objective.
middleText("New Objective: " + objectiveText); //Display text on screen.
}
public void FinishObjective(string shortObj)
{
var value = objectives.First(x => x.ObjectiveName.Contains( shortObj ));
string completedTask = value.objectiveDescription;
middleText("Completed Objective: " + completedTask); //Display text on screen.
objectives.Remove(value); //Remove value. (Which doesn't find anything for some reason, so it can't delete anything from the list.)
}
And then in another class, I have it make a new objective, like this.
GameController.gameController
.GetComponent<GameController>()
.NewObjective("Foobar.", "foo"); //First is the Quest description,
and the second is the name for easy deletion.
(I've also included a objectiveID
method but I've omitted for ease.)
In that same class, when the player completes something, I have this.
GameController.gameController
.GetComponent<GameController>()
.FinishObjective("foo"); //This has two possible methods,
the object ID (if defined) or the name of the objective.
What is going on, what am I doing wrong and what can I do to fix this? Thanks for the help.
Edit: There is no actual error. It's just that it doesn't find anything, while there is something. Objectives is just easily defined as List objectives = new List(); inside the class.
This is the objectives class:
public class Objective : MonoBehaviour
{
public string objectiveDescription;
public string ObjectiveName;
public int ObjectiveID;
public Objective(string objective, string objectiveName)
{
objectiveDescription = objective;
ObjectiveName = objectiveName;
}
public Objective(string objective, string objectiveName, int objectiveID)
{
objectiveDescription = objective;
ObjectiveName = objectiveName;
ObjectiveID = objectiveID;
}
}
Upvotes: 1
Views: 88
Reputation: 2923
You may need to implement something like IEquatable inside your Objective class. You are asking the computer to check if ObjectiveName contains shortObj but it does not know how to make this comparison. is ObjectiveName a string? List of strings? a(n) Objective class? If it's a list of strings that we are calling "objective names" then the expected element would be a string (something like shortObj.ObjectiveName assuming that's a string). If ObjectiveName is a list of Objective classes and you are asking if this list contains a specific Objective element called shortObj, then you'll need to implement IEquatable into your Objective class.
EDIT In light of recent comments, try something like:
var value = objectives.AsEnumerable().Where(x => x.ObjectiveName == shortObj).FirstOrDefault();
Upvotes: 2