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
0
Question by Naina · Mar 07, 2012 at 10:10 AM ·

Moving the head of the snake

I am newbie on Unity 3d.I have started practising with a snake game. I have to move snake head forward first.If i press any key then the snake should start moving ahead.For its head i have taken simply a cube. Here is the code. Please tell me where i am doing mistake.

 public class SnakeMove: MonoBehaviour {
 
 public bool Move_Up;
 public bool Move_Below  ;
 public bool Move_Right;
 public bool Move_Left;
 
 public body first_body;
 
 public float time_movement = .5F;
 public float following_movement;
 
 
 // Use this for initialization
 void Start () {
     Move_Up = false;
     Move_Below = false;
     Move_Right = false;
     Move_Left= false;
     following_movement = Time.time + time_movement;
 }
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         Move_Up = true;
         Move_Below = false;
         Move_Right = false;
         Move_Left = false;
     }
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         Move_Up = false;
         Move_Below = true;
         Move_Right = false;
         Move_Left = false;
     }
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         Move_Up = false;
         Move_Below = false;
         Move_Right = true;
         Move_Left = false;
     }
     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         Move_Up = false;
         Move_Below = false;
         Move_Right = false;
         Move_Left = true;
     }
 
     if (Time.time > following_movement)
     {
         MoveHead();
     }
 
 }
 
 void MoveHead()
 {
     if (Move_Up)
     {
         first_body.move(this.transform);
         this.transform.position += transform.forward *transform.localScale.z;
     }
     if (Move_Below)
     {
         first_body.move(this.transform);
         this.transform.position += -transform.forward * transform.localScale.z;
     }
     if (Move_Right)
     {
         first_body.move(this.transform);
         this.transform.position += transform.right * transform.localScale.z;
     }
     if (Move_Left)
     {
         first_body.move(this.transform);
         this.transform.position += -transform.right * transform.localScale.z;
     }
     following_movement = Time.time + time_movement;
 }
 }

The error is:

Assets/Scripts/SnakeMotion.cs(92,8): error CS0246: The type or namespace name `body' could not be found. Are you missing a using directive or an assembly reference?

Should have I to make a body script also? N what should be included in that script? Or Can i run without making separate Body script also? Sorry for my english.

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 Kleptomaniac · Mar 07, 2012 at 11:09 AM

I'm not super great with C#, but I think it's pretty simple. You're problem is here: public body first_body; You are declaring a variable of type body, however no type of that kind exists. I'm guessing from your code that what you'd want instead (remember I've barely ever used C#): public GameObject first_body; That should work.

Also, though, why are you doing all your movement in another function which is only called as a result of Time.time? You should really be doing all your movement in Update() instead of using the complicated boolean structure you have there. However I would be of no help reformatting your code, because of my lack of knowledge in the language ... ;)

Anyway, I have no idea if that helped. Hopefully it did. I should really get to learning C# properly ... I'm a JScript man first and foremost, I suppose ...

Klep

Comment
Add comment · 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
0

Answer by Meltdown · Mar 07, 2012 at 12:45 PM

Your problem is this line..

 public body first_body;

'body' is not a recognised type. Either you need to create a class for it, or if it's a gameobject change the line to..

 public GameObject first_body;
Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to make a stargate work ?? 1 Answer

Flash player slows down when unity player is installed 0 Answers

Assigned variable 1 Answer

Unity3D Crashes when baking terrain 1 Answer

Monodevelope auto complete problem? 1 Answer


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