Snake Game problems - errors

Hello, I am new to coding and would like some help. I am making simple snake type game by following some tutorials and changing as I go along. However, the closer I get to finishing the more errors appear. Here is a copy of my code:

#pragma strict

var food : GameObject;
private var food : int;
var tail : GameObject;
private var lastTail: GameObject;
var showHigh : boolean;
var speed = 10.0;
var rotateSpeed = 10.0;
var wall : GameObject[];

function Start () {
	showHigh = false;
	var i : int = 0;
	while(i < 5); //GJD: you don't want a semicolon here
//GJD: I think there should be a function here?
	food = 0;
	lastTail = gameObject;
	if(PlayerPrefs.GetInt("Food")); //GJD: no semicolon
		PlayerPrefs.SetInt("Food", 1);

function Update ()
//GJD: You need an opening brace
	transform.Rotate(Vector3(0,-20 * rotateSpeed * Time.deltaTime,0));
	transform.Rotate(Vector3(0, 20 * rotateSpeed * Time.deltaTime,0));

	transform.Translate(Vector3.forward * speed * Time.deltaTime);
//GJD: If this is the end of the Update function you need a closing brace
	function placeFood (){
	var foodname	= Instantiate(food[(Random.Range(0,food.Length))], //GJD: You might need to say foodname is an object/transform/gameobject
		 new Vector3(Random.Range(-48, 96), 1.5, 
		 Random.Range(-23, 22)), 
		 Quaternion.identity) as GameObject; = "Food";
		 function OnCollisionEnter (collision : Collision)
		 if( == ("Food")); //GJD: Do you get paid each time you enter a semi-colon?  
		  {//GJD: This brace isn't needed
		  else if( = = "Tail" || "Wall"); //GJD: That would be == not = =, and another semi-colon?
		 GUILayout.Label("Game Over"); //GJD: This can only be used inside OnGUI
		 if(GUILayout.Button("Start Again")); //GJD: Ditto, and more semi-colons at the end of an if, but no brace
		 }//GJD: I've lost track of which opening brace this needs to match
		 function addTail(){
		 var newTail : GameObject = Instantiate(tail, transform.position(rigidbody.velocity * 100), Quaternion.identity); = "Tail";
		 newTail.GetComponent(SmoothFollow).target = lastTail;
		 lastTail = tail;
		 function onGUI (){
		 if (showHigh && GUILayout.Button("Show Highscore")); //GJD: Not sure if I have said or not, but ifs don't have semi-colons, but they do have opening braces
		 	showHigh = true;
		 	{//GJD: This isn't needed
		 	else if (showHigh && GUILayout.Button("Hide Highscore"));  //GJD: You know what's wrong here
		 		showHigh = false;
		 		{ //GJD: You know what's wrong here
		 		GUILayout.Label(PlayerPrefs.GetInt("Food") + food);
    //GJD: probably missing a closing brace, you code is so badly formatted it's impossible to tell

This is the only error I have that appears at the moment : Assets/Own Scripts/Moving.js(23,14): BCE0044: expecting :, found ‘=’.

Thanks in advance.

James McNulty

You have a gazillion problems. I’ve editted your script and added some comments. Maybe watch some of the scripting tutorials on the Learn site.

A common mistake is you do not write if statements correctly:

if (condition){
   // code it execute if the condition is matched

Note the opening brace following the condition, and the closing brace that is aligned with the if. This makes the code readable. You can also use:

if (condition)
   // code it execute if the condition is matched

I personally think opening braces on a line all by themselves makes the code longer.

Also every opening brace has a matching closing brace. Functions start and stop with them:

function doit() {
    // code for the function

Note that all of this is general programming lessons, it’s totally unrelated to Unity.

Okay, so the error is in this line of code: food = 0;

As far as I know, you can’t change a gameObject to 0.

I’m sorry, but I can’t help you further than that :slight_smile:

Thanks, Andreas :slight_smile: