- Home /
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
placeFood();
}
//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
if(Input.GetKey("a")){
transform.Rotate(Vector3(0,-20 * rotateSpeed * Time.deltaTime,0));
}
if(Input.GetKey("d")){
transform.Rotate(Vector3(0, 20 * rotateSpeed * Time.deltaTime,0));
}
if(Input.GetKey("w")){
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;
foodname.name = "Food";
}
function OnCollisionEnter (collision : Collision)
{
if(collision.gameObject.name == ("Food")); //GJD: Do you get paid each time you enter a semi-colon?
addTail();
Destroy(collision.gameObject);
placeFood();
}
{//GJD: This brace isn't needed
else if(collision.gameObject.name = = "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
Application.LoadLevel(Application.LoadedLevel);
}//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);
newTail.name = "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
if(showHigh)
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
Answer by Graham-Dunnett · Jul 10, 2013 at 09:15 PM
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.
I've tried to be very helpful, and gentle with you. I'd strongly urge you to leave this script for a while, and read up on Javascript. There are coding conventions that you are completely missing, particularly the way code is written to be readable.
Thanks for your help and advice. I will go away and read materials. And sorry for all the mistakes :|
No need to apologise, we all started out as beginners.
@James: Start smaller. That way you can learn each piece at a time and learn what the (often cryptic) error messages mean. Just write a few lines at first, put in statements like Debug.Log("some meaningful text here")
and test it to see if you're getting the expected results.
Answer by AndreasX12 · Jul 10, 2013 at 09:32 PM
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 :-)
Thanks, Andreas :-)
That's true. I missed that in the avalanche of other problems. ;-)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to upload a game to the internet 1 Answer
Multiplayer Script error 2 Answers
Why do these errors keep showing up with C#? Im a beginner with coding 1 Answer
HighScore analytics 0 Answers