I’m following this tutorial to get the closest object to the player, and I’m getting a return type error.
Here’s the script that’s showing the error:
using System.Collections;
using System;
using UnityEngine;
public class FindClosestObject : IComparer {
private Transform compareTransform;
public DistanceComparer (Transform compTransform) {
compareTransform = compTransform;
}
public int Compare (object x, object y) {
Collider xCollider = x as Collider;
Collider yCollider = y as Collider;
Vector3 offset = xCollider.transform.position - compareTransform.position;
float xDistance = offset.sqrMagnitude;
offset = yCollider.transform.position - compareTransform.position;
float yDistance = offset.sqrMagnitude;
return xDistance.CompareTo(yDistance);
}
}
The error is at line 9, on the DistanceComparer function.
The full error is “Class, struct, or interface method must have a return type”.
The video is pretty recent, so I don’t understand why the person is not getting an error, but I am.
You shaped the thing on line 9 like a constructor but constructors must always have the same name as the class they construct. So the compiler can’t necessarily guess whether you have an incorrectly-named constructor or a method without a return type. Compilers aren’t good at guessing intent but humans are.
What I think really happened is you wanted the thing on line 9 to be a constructor.
public FindClosestObject(Transform compTransform) {
compareTransform = compTransform;
}
1 Like
Thanks, that solved it.
I just realized I’m also getting some errors in another script from that tutorial.
Here’s the script:
using System;
using UnityEngine;
public class Sense : MonoBehaviour {
public float checkRadius;
public LayerMask checkLayers;
private void Update {
if (Input.GetKeyDown (KeyCode.Space)) {
Collider[] colliders = Physics.OverlapSphere(transform.position, checkRadius, checkLayers);
Array.Sort(colliders, new DistanceComparer(transform));
foreach (Collider item in colliders) {
Debug.Log(item.name);
}
}
}
public void OnDrawGizmos() {
Gizmos.DrawWireSphere(transform.position, checkRadius);
}
}
And the errors are:

Line 9 is the Update function, line 10 is the “if” statement, and line 18 is the closing curly bracket of Update.
I definitely know I didn’t misplace a bracket, and I’ve checked multiple times now. I don’t understand the error for Update, as when I remove the “void” return type, it seems to break and the console tells me that every bracket is an unnexpected symbol. I tried using other return types, and while that fixes it, I don’t think it’s what I’m supposed to do. For example, setting the return type to “int” removes the error.
I also don’t understand the error at line 10, as I have never worked with the “set” and “get” accessors in C#, and I have only come across them a few times in other people’s scripts.
You’re missing () after Update
1 Like
You forgot the parenthesis in your update function:
private void Update(){
}
oh, sorry, I should have refreshed before posting.
1 Like