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 Bolt · Dec 26, 2013 at 05:50 PM · gameobjectarrayssnakefollowingtail

snake problem

I'm trying to snake with unity. I have a problem with the creation of the body. As you know every time you eat the food the body of the snake gets longer. I make an array of GameObject and add a piece every time. This is my code. The problem is that I do not know how to add an object to an array of objects. I've tried using the Add and push but does not work. In addition, each piece of the body should follow the previous positions that the problem should have a 1 second delay otherwise change position immediately.

 #pragma strict
 
  
 
 //VARIABILI PUBLIC
 
 public var direction : String = 'Right';
 
 public var currentDirection : String; 
 
 public var input;
 
 public var cubo : GameObject[];
 
 public var food : GameObject;
 
 var Snake : GameObject;
 
  
 
 function Start () {
 
  
 
       var position: Vector3 = Vector3(Random.Range(0, 10.0), Random.Range(0, 5.0), 0);
 
        Instantiate(food, position, Quaternion.identity);
 
 }
 
  
 
 function Update () {
 
  
 
 if(Input.GetMouseButtonDown(0))
 
      {
 
      //Debug.Log("premuto");
 
     var agg = Instantiate(Snake, new Vector3(cubo[0].transform.position.x, cubo[0].transform.position.y , cubo[0].transform.position.z), Quaternion.identity);
 
      //cubo.Add(agg);
 
      }
 
         var horizontal = Input.GetAxis("Horizontal");
 
         var vertical = Input.GetAxis("Vertical");
 
  
 
     if(horizontal >0 && currentDirection != 'Left')  
 
       {
 
          direction = 'Right';
 
          
 
       
 
       }
 
       
 
          if(horizontal < 0 && currentDirection != 'Right')  
 
       {
 
          direction = 'Left';
 
          
 
       
 
       }
 
       
 
       if(vertical < 0 && currentDirection != 'Up')  
 
       {
 
          direction = 'Down';
 
          
 
       
 
       }
 
       
 
       if(vertical > 0 && currentDirection != 'Down')  
 
       {
 
          direction = 'Up';
 
          
 
       
 
       }
 
       
 
       move();
 
      
 
          
 
  
 
  
 
 }
 
  
 
      function move(){
 
      
 
              if(direction == 'Right'){
 
               
 
               
 
               cubo[0].transform.Translate(0.03,0,0);
 
               
 
               currentDirection = direction;
 
               draw();
 
              }
 
              
 
              if(direction == 'Left'){
 
               
 
               
 
               cubo[0].transform.Translate(-0.03,0,0);
 
               
 
               currentDirection = direction;
 
               draw();
 
              }
 
              
 
              if(direction == 'Up'){
 
               
 
               
 
               cubo[0].transform.Translate(0,0.03,0);
 
               currentDirection = direction;
 
               draw();
 
           
 
              }
 
              
 
               if(direction == 'Down'){
 
               
 
               
 
               cubo[0].transform.Translate(0,-0.03,0);
 
               
 
               currentDirection = direction;
 
            
 
               draw();
 
              }
 
      
 
      
 
      }
 
      
 
      function draw()
 
      {
 
         for(var i =0; i<=cubo.Length; i++){
 
         
 
          var prev = cubo[0].transform.position;
 
         
 
          cubo[i+1].transform.position= cubo[i].transform.position;
 
         
 
        
 
      }
 
      }
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 highpockets · Dec 26, 2013 at 06:47 PM 0
Share

look at this:

http://answers.unity3d.com/questions/13470/add-an-object-to-array.html

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

How to Narrow Down an Array casted from FindObjectsOfType 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

Basic object creation in arrays! Syntax error! 1 Answer

How could I collect all gameobject Materials? 1 Answer

Destroy created children of objects 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