"Method must have a return type"
The error says
"Method must have a return type" Around the AddX and MinusX. Above the Add and Minus I have the code that is meant to update in realtime the position of the object. Where it gives me 2 errors on all 3 lines of code. It says "Invalid token '=' in class, struct, or interface member declaration" In the same line it says "Invalid token '(' in class,struct, or interface member declaration"
Aside from my main question if anyone knows how I can have the player select an object when he looks at it please let me know!
using System.Collections; using UnityEngine; using UnityEngine.UI;
public class Forge_Menu : MonoBehaviour {
public int x;
public int y;
public int z;
public Text XCurrent;
public Text YCurrent;
public Text ZCurrent;
public GameObject Selected;
void Start () {
x = PlayerPrefs.GetFloat ("ObjectX", transform.position.x);
y = PlayerPrefs.GetFloat ("ObjectY", transform.position.y);
z = PlayerPrefs.GetFloat ("ObjectZ", transform.position.z);
}
void Update () {
if (PlayerPrefs.GetInt ("ObjectX") != x) {
PlayerPrefs.SetInt ("ObjectX", x);
PlayerPrefs.Save ();
Debug.Log ("Save completed. X = " + x);
}
if (PlayerPrefs.GetInt ("ObjectY") != y) {
PlayerPrefs.SetInt ("ObjectY", y);
PlayerPrefs.Save ();
Debug.Log ("Save completed. Y = " + y);
}
if (PlayerPrefs.GetInt ("ObjectZ") != z) {
PlayerPrefs.SetInt ("ObjectZ", z);
PlayerPrefs.Save ();
Debug.Log ("Save completed. Z = " + z);
}
transform.Translate (x, y, z);
}
XCurrent.Text = x.ToString();
YCurrent.Text = y.ToString();
ZCurrent.Text = z.ToString();
public AddX(){
x++
}
public MinusX(){
x--
}
public AddY(){
y++
}
public MinusY(){
x--
}
public AddZ(){
z++
}
public MinusZ(){
z--
}
}
Also if this is what I think it is, PlayerPrefs is a terrible way of saving game data. You should serialize data to a file (check this series out).
PlayerPrefs should be used only for configuration settings like language and such, as it writes directly to the registry and can become very heavy for the system. It also doesn't leave the choice of where it's saved to the user, which means their C:\ disk could get filled for no apparent reason. (on windows at least)
Answer by Saryk360 · Jun 30, 2017 at 02:25 PM
The error tells you exactly what is wrong. Your functions on lines 40 to 60 don't have a return type : since in your case they don't return a value, the return type is void.
Declare them as
public void AddX(){
x++
}
public void MinusX(){
x--
}
public void AddY(){
y++
}
public void MinusY(){
x--
}
public void AddZ(){
z++
}
public void MinusZ(){
z--
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612125216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
error CS0120: An object reference is required to access non-static member `ScoreManager.score' 1 Answer
How to make an enemy 2d chasing player? 1 Answer
Wrong after Update? 0 Answers
warning CS0618 with unity ngui 0 Answers
Same Q as before but it has been a while and nones replyed so im reposting 2 Answers