- Home /
Cannot implicitly convert type `System.Action' to `bool'
Special Thanks to Marrrrk for this example script:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class SelectionManager
{
public List<Unit> _selection = new List<Unit>();
public Action<Unit> UnitSelected;
public Action<Unit> UnitDeselected;
public void Select(Unit unit)
{
if (_selection.Contains(unit))
return;
_selection.Add(unit);
if (UnitSelected)
UnitDeselected(unit);
}
public void Deselect(Unit unit)
{
if (!_selection.Contains(unit))
return;
_selection.Remove(unit);
if (UnitSelected)
UnitSelected(unit);
}
public static SelectionManager Default = new SelectionManager();
}
There error is when the following line is called if (UnitSelected) UnitSelected(unit);
I've never used List's of Action's before, and I can't quite figure this out, so thanks for any help.
Answer by ArkaneX · Sep 12, 2013 at 07:43 PM
I guess that this if is there to check if particular action was assigned and is not null. In this case you have to change it to:
if(UnitSelected != null)
{
UnitSelected(unit);
}
and the same with the UnitDeselected.
Also, please note that you have an error inside Select method - in if condition you check UnitSelected and then you call UnitDeselected.
Alright, I'll look into it. Like I said, I honestly have no idea how this script works, and considering it's NOT WOR$$anonymous$$ING at the moment, I can't really study it to figure out the functionality. I've never used the Unit class before, so I guess I'm going to have to look into that as-well.
I don't know what this Unit class is as well, but in context of this script it doesn't really matter. You have two variables defined there with type Action. Action is a delegate which is able to call a method with a single parameter. Please read a bit more about it on $$anonymous$$SDN page.
If you don't assign anything to for example UnitSelected variable, then nothing special happens. But if you assigned some method to UnitSelected, e.g. Warcry, then after selecting, Warcry method will be called. The page I linked above explains it a bit better than I :)