- Home /
Can't override GetHashCode (cannot change return type when overriding method int UnityEngine.Object.GetHashCode")
Hello, in my MonoBehaviour script i need to override GetHashCode. But whenever I do, I get the error cannot change return type when overriding method int UnityEngine.Object.GetHashCode"
(Isn't GetHashCode located inside the System namespace?)
My overriding:
public override int GetHashCode()
{
return index.GetHashCode();
}
The error no longer exists if I remove the using UnityEngine header from the top. I tried to explicitly tell it what to override, it didn't work.
What's going on here, how can I override GetHashCode here?
Thanks.
EDIT: I tried a brand new empty class, same result.
using UnityEngine;
public class Entry : MonoBehaviour
{
public override int GetHashCode()
{
return base.GetHashCode();
}
}
I also tried it in another project, it didn't work.
Just tested it on my machine, and it works without any problem. GetHashCode is overriden in UnityEngine.Object, and that's why you get the error related to this class.
Could you please test this on a new Unity project, with just one script, index variable initialization and your override?
This is really strange. Please do a small test - change 'int' in override definition to 'long'. Under normal condition, compiler should now fail with message:
CS0508: Entry.GetHashCode(): return type must be 'int' to match overridden member 'UnityEngine.Object.GetHashCode()'
And one more question - is your error generated during compilation, or during execution?
Thanks ArkaneX. Everything you said was correct. It did print that error when I changed it to long. But when you asked me about whether the error was generated during run or compile time, I got the notion to build the solution, that's when I got a successful build and realized it was a R# mistake. Wish I could upvote your comments.
You should be able to upvote - there's a small 'thumb up' icon before username in all comments :)
Answer by vexe · Aug 17, 2013 at 12:18 PM
As ArkaneX said, it's working fine no problem. I'm sorry for the confusion, my original code turned out to be working fine, but it was a false Resharper error all along! I got a successful build even though R# is saying that there's an error. I should never trust R# by its own without building.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnTriggerEnter2D(collidertype coll) C# 2 Answers
Setting variable when method called 1 Answer
what is wrong whis this? 1 Answer