I am a newbie at unity(first forum post on here) but i keep getting this error when i try to run a really basic script
Unexpected symbol public at (13,25)
using UnityEngine;
using System.Collections;
public class myScript : MonoBehaviour {
public int a = 13;
public int b = 5;
void Start () {
if (a==13)
{
public int c = a+b;
Debug.Log("Answer is: "+ c);
}
}
// Update is called once per frame
void Update () {
first off, use code tags. You had the right idea by putting the dashes, but the forum has something built in for it. See this thread for details:
As for your problem, where you have the line inside Start that goes:
public int c = a+b;
Thatās your problem.
Just remove the āpublicā from that line.
The āpublicā keyword is an access modifier. A function/method level variable can not be accessed from outside the function, so it has no need for a access modifier.
Only types and members of types (fields, properties, methods, events, etc) get access modifiers.
nvm i got that fixed but when i put public int c=a+b outside of the functions it gives me the error a field initializer cannot reference the nonstatic fieldā¦
well if you declare āpublic int aā outside of a function, youāre making a field. When declaring the field, you can give it a default value to initialize as. That value must be a value knowable at initialization⦠you canāt set it to the sum of other fields (unless those fields were static, because static fields initialize before instance fields).
Donāt make a = b+c at initialization. Set that in Awake or Start.
Iām not sure what roblox uses, but here weāre using Mono/.Net, youāre creating a class.
A class is a blueprint for an object. And that object when created is attached to a GameObject in your scene. Note this āobjectā being attached is NOT the same as an object in your scene. Itās an object in the sense of OOP object identity.
When a class is constructed into an instance/object it goes through some basic steps.
Memory on the heap is allocated for the state of the object. Itās enough memory to hold the fields (class level variables, not declared in functions, and arenāt static/const).
those variables have their initial values set in order from top to bottom, if any of those fields call another constructor, that object is initialized first.
a constructor function is called on the object (note, your script files in Unity shouldnāt have constructor functions⦠they arenāt called on the main thread, so can cause big errors if used)
After this, unity has its own steps that it then does:
the fields for it are now set again to what the serialized data that is associated with this object says it aught to be
If the class is a MonoBehaviour it then continues on to:
// Use this for initialization
void Start () {
StartGame ();
}
void StartGame();
max = max + 1;
print (āWelcome to number wizard!ā);
print (āPick a number in you head but donāt tell meā);
print ("The highest number you can chose is " + max );
print ("The lowest number you can chose is " + min );
print ("Is you number higher or lower than " + guess);
print (āUp key for higher, down key for lower, enter for the sameā);
void NextGuest () {
guess = (max + min) / 2;
print ("Is it higher or lower than " + guess);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
min = guess;
NextGuest ();
}else if (Input.GetKeyDown(KeyCode.DownArrow)) {
print (ādown arrow pressedā);
max = guess;
NextGuest ();
}else if (Input.GetKeyDown(KeyCode.Return)) {
print (āI won!!!ā);
}
}
}
Can anyone help me with this. It says I have 11 unexpected symbols. If you can help it would be much appreciated.
Anything look weird? Like maybe a misplaced semi-colon?
From there the rest of the body of āStartGameā is not wrapped correctly, making āNextGuestā malformed.
You may notice if you had it formatted with code tags:
using UnityEngine;
using System.Collections;
public class NumberWizard : MonoBehaviour {
int max = 1000 ;
int min = 1 ;
int guess = 500;
// Use this for initialization
void Start () {
StartGame ();
}
void StartGame();
max = max + 1;
print ("Welcome to number wizard!");
print ("Pick a number in you head but don't tell me");
print ("The highest number you can chose is " + max );
print ("The lowest number you can chose is " + min );
print ("Is you number higher or lower than " + guess);
print ("Up key for higher, down key for lower, enter for the same");
void NextGuest () {
guess = (max + min) / 2;
print ("Is it higher or lower than " + guess);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
min = guess;
NextGuest ();
}else if (Input.GetKeyDown(KeyCode.DownArrow)) {
print ("down arrow pressed");
max = guess;
NextGuest ();
}else if (Input.GetKeyDown(KeyCode.Return)) {
print ("I won!!!");
}
}
}
Note that usually errors/exceptions will point directly at the offending line of code.