landonbradlee landonbradlee - 1 month ago 13
C# Question

Must have a body because it is not marked abstract, extern, or partial

Not entirely sure why I'm getting this error. Looked up and down the code for a few hours but I assuming it's a simple fix. Some of the code is copied over from a video and his works perfectly fine, so I don't know why it'd cause me any issues:

using UnityEngine;
using System.Collections;

public class GoldPerSec : MonoBehaviour {


public UnityEngine.UI.Text gpsDisplay();
public Click click;
public ItemManager[] items;


void Start() {
StartCoroutine(AutoTick());
}

void Update() {
gpsDisplay.text = GetGoldPerSec() + " gold/sec";
}

public int GetGoldPerSec(){
int tick = 0;
foreach (ItemManager item in items) {
tick += item.count * item.tickValue;

}
return tick;
}

public void AutoGoldPerSec() {
click.gold += GetGoldPerSec();
}

IEnumerator AutoTick() {
while (true) {
AutoGoldPerSec();
yield return new WaitForSeconds(1);
}
}
}

Answer

The problem is in

public UnityEngine.UI.Text gpsDisplay();

That is a method declaration, but there's no code inside the method.

It's unclear from the context what that should actually be. If it's a method, you can either implement it in this class, like

public UnityEngine.UI.Text gpsDisplay()
{
     return <something useful>;
}

Maybe it shall be a method and you want to implement it in a subclass, then mark the class as abstract:

public abstract class GoldPerSec : MonoBehaviour

Perhaps it should have been a field, then use

public UnityEngine.UI.Text gpsDisplay;

without the parentheses.