- Home /
Implementing interfaces in C# Unity - am I missing something?
I must be missing something obvious, I cant seem to implement the IEquatable interface in MonoDevelop and Unity..
The error I am getting is:
The type or namespace name IEquatable
1' could not be found. Are you missing a using directive or an assembly reference?
Here is the start of the class..
using UnityEngine;
using System.Collections.Generic;
public class Item : IEquatable<Item>
{
private string _name;
private int _cost;
private RarityTypes _rarity;
private Texture2D _icon;
public override int GetHashCode()
{
return this.Item.GetHashCode();
}
public override bool Equals(object obj)
{
Item item = obj as Item;
if (item != null)
{
return Equals(item);
}
else
{
return false;
}
}
Answer by BlueRaja_2014 · Jun 28, 2013 at 06:15 AM
You may need to specify the namespace for IEquatable<​T​>
in Unity:
public class Item : System.IEquatable<Item>
{
...
Also make sure you actually implement the interface...
public bool Equals(Item item)
{
...
}
Thanks. Although I still have a problem. It says I am not implementing equals. I have tried both public bool Equals() and public override Equals() both still give errors - any idea?
Scratch that when I correctly used my type Item ins$$anonymous$$d of object has used in the reference code on $$anonymous$$SDN its working, thanks again!
@madmike6537 Yes, that's why I had that second paragraph :) The public override bool Equals(Object obj)
method and the public bool Equals(Item item)
are two different methods. The first is an override from the Object
class (the base class to all classes), the second is from the IEquatable<​T​>
interface.
If you implement one, you should implement both, as well as `GetHashCode()`.
The reason for the disparity is that the first method has been around since the beginning of .Net, while generics (and hence the second method) were not introduced until .Net 2.0.
Answer by Jessy · Jun 28, 2013 at 04:02 AM
using System;
"object" exists in both System and UnityEngine; if you include System you'll have to specify UnityEngine when you need a UE object.
Just something to note. You can also :System.IEquatable if you just need it for the one thing.
I just need it for the Equals method. But using.System works. Thanks!
Your answer
Follow this Question
Related Questions
Is there still have the old UI color theme in Unity 3 Pro? 2 Answers
Flash inside Unity 3 Answers
command system with 3d gui interface 0 Answers
Not sure where to start... Riichi Mahjong game 0 Answers
Project view thumbnails (Prefabs/Materials/etc...) 0 Answers