Unexpected symbol `public`

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.

2 Likes

OOOOOOOOOO thank you i really appreciate it.

Is the script attached to a GameObject, is that GameObject active, you played the game?

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.

oooo i see what you mean now so if i do

public int c; at initialization

then c=a+b; in start or awake then the other variables will be made before i declare c.

1 Like

yeah

The idea is completely new to me i am coming from roblox where the script is ran line by line.

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.

http://en.wikipedia.org/wiki/Identity_(object-oriented_programming)

When a class is constructed into an instance/object it goes through some basic steps.

  1. 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).

  2. 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.

  3. 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:

  1. 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:

  1. Awake is called

  2. OnEnable is called (if the script is enabled)

  3. Start is called (if the script is enabled)

Coming from a procedural background to an OOP background is weird at first. But once you get used to it it’s like magic.

yeah i am just going to chill for first few days and try to do what i did on Roblox on Unity3D i think that will clear up alot of things.

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!!!ā€);

}
}
}

Can anyone help me with this. It says I have 11 unexpected symbols. If you can help it would be much appreciated.

Use code tags!

Also look at this line right here:

void StartGame();

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.