Is it possible to set a 'return' type in an abstract class?

I need to get an ID from an abstract class that I’m using to create different interactive objects in the world that you can click on. However, I know this is an issue for abstract classes. Is there a way to do it?

image

Why this would be an issue for abstract classes specifically?

If you remove the abstract keyword, does the error still exist? If it does, it is not something specific to abstract classes.

Try looking at the error by hovering over the squiggly line, what does it say? The error message should give you an idea of what is wrong, more specifically: What is this ID? Have you declared it anywhere? The compiler will complain for anything that is not a keyword or hasn’t been declared properly.

Also: Write code in code tags, don’t use pictures.

This is completely legal code:

public abstract class Interactor : MonoBehaviour
{
	[SerializeField]
	private int _id;
	
	internal int GetID() => _id;
}

Abstract classes can still have their own implementations, but they of course can’t be instanced and a non-abstracted derived type is still required.

THANK YOU! That’s exactly what I wanted! No more messy return methods!

image