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 Ninja7000 · May 31, 2021 at 08:07 PM · c#unity 2dgamesnakeconfused

making snake game but the code is confusing

Hi, so I've started to learn how to make snake game in unity today from a toutorial but the code was kind of bewildering especially for me beacuase I'm a begginer at unity and I'm still eleven years old. So here is the code in the toutorial that perplexed me

     void Move() {
     // Save current position (gap will be here)
     Vector2 v = transform.position;
 
     // Move head into new direction (now there is a gap)
     transform.Translate(dir);
 
     // Do we have a Tail?
     if (tail.Count > 0) {
         tail.Last().position = v;
     }

so basically what we are doing is we have created the head of the snake and we have also made a list of tales for the snake's body and the idea is that when the head for the snake moves in a certain direction the last element in the tales list will move to the gap that the head had made; so where the snake's head was before, but in the toutorial they didn't say that the last element in the tales list will move to the gap that the snake's head had made, they said that the last element in the tale's list will move to the position(transform.position) of the snake's head not to the gap that the snake's head had made. so that was confusing beacuse when I ran it what happend was that when the head moved in a certain direction the last element in the snake's body moved to the gap that the head's snake had made not to the position of the snake's head because in the code they said that they will move the last element of the tale's list to the transform.position of the snake's head. Do you know why I'm wrong? and do you know why that happens and how it works. thank you.

Comment
Add comment · Show 1
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 Ninja7000 · May 31, 2021 at 08:13 PM 0
Share

And I forgot to say that I called the Move fuction in the start method like this InvokeRepeating("Move", 0.3f,0.3f);

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Jun 01, 2021 at 03:36 PM

When captures the position of the head in this line:

 Vector2 v = transform.position;

it is looking at where the snake's head position is before it has moved. When the head does finally move in this line

 transform.Translate(dir);

The head is at a different position but v still has the value of the old head position. So when we move the tail to position v it goes to where the head was before it moved.


Now you may ask yourself why v refers to the old position rather then the new position. Look at the following code:

 int i = 0;
 int j = i;
 i = i + 1;
 //At this point, j = 0 and i = 1

When we call j = i, j is getting a copy of the value of i, but does not refer to the value of i directly. So if i changes later, j does NOT change. The same thing happens with Vectors or any other struct in C#.

 Vector2 i = new Vector2(0, 0);
 Vector2 j = i;
 i = i + new Vector2(1, 1);
 //At this point j = (0, 0) and i = (1, 1)

So hopefully that answers your question as to why the tail moves to the gap where the head was rather than on top of the head.


It is important to note that this is different for any Class types in C#. Consider the following code

 class MyClass {
     public int value;
 }
 
 MyClass a = new MyClass();
 a.value = 0;
 MyClass b = a;
 a.value = a.value + 1;
 //At this point a.value = 1 AND b.value = 1

This is one of the very important differences between Classes and Structs in C#. Structs are pass by value and classes are pass by reference. Vector3's are structs, and are therefore passed by value, meaning that when you pass one struct to another the seconds struct gets a copy of the original data, not the original data itself.

Comment
Add comment · Show 2 · 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 Ninja7000 · Jun 01, 2021 at 07:29 PM 0
Share

Ok thank you very very very much now I understand it too well

avatar image Ninja7000 · Jun 01, 2021 at 07:40 PM 0
Share

I thought structs are reference types that's why I got confused

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

145 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

My script only partially works 2 Answers

Disable teleporter after being used 1 Answer

Get Random Number for each player from Dice 1 Answer

Multiple Cars not working 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