Setting Variable as Public

hello i am quite new to unity and C# itself … so learning my way into this beautiful field of making games …
Please guide me i would be really grateful …
Ok i have this issue i am facing …
first of all sometime monodevelop does not allow me to set the variable as public …the word public does not exist in the intellisense and even if i try to write down manually it does not update in the inspector editor … whats going on …

another issue is say i have variable speedX i declare and its all set…

public float speedX;

void Start () 
{
	print (speedX);
}

now i decide to initiate it by giving it a value … which if i try to it simply
public float speedX;
speedx = 2f;

void Start () 
{
	print (speedX);
}

i get this

78181-issue.png

Remove your separate “speedX = 2f;” line and just add it to the float.

public float speedX = 2f;

You can’t modify the value of a variable outside a function like that. If you want the default value to be 2 then do what I said above, or put the “speedX = 2f;” in your start function if you want it to be set upon starting the script.

You could leave it as is, however as Temseii stated you can’t declare a variables value outside a function.

public float speedX;

void Start(){
    print (speedX);
}

And then go on to declare the variable from the Inspector, this would also allow for it to be a more dynamic feature instead of having the value “hardcoded” Hope I helped :wink: