Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by James_McNulty · Jul 10, 2013 at 09:08 PM · errorgamebeginnersnake

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Jul 10, 2013 at 09:27 PM 0
Share

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.

avatar image James_McNulty · Jul 10, 2013 at 09:58 PM 0
Share

Thanks for your help and advice. I will go away and read materials. And sorry for all the mistakes :|

avatar image Graham-Dunnett ♦♦ · Jul 10, 2013 at 10:06 PM 0
Share

No need to apologise, we all started out as beginners.

avatar image s_guy · Jul 10, 2013 at 10:23 PM 0
Share

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

avatar image
0

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 :-)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Jul 10, 2013 at 09:33 PM 0
Share

That's true. I missed that in the avalanche of other problems. ;-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges