• 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 SecondMatter · Sep 23, 2012 at 05:46 PM · c#gameobject

How to move a GameObject after it has been initiated?

Hi guys,

I'm just starting to learn Unity and am developing Snake as a way to learn the engine and C#.

I'm having a lot of trouble getting the snake body to work. At the moment, I have a snake head (just one cube that I can control), and I want to write a script so that when I press spacebar, it creates a body segment. I can get the script to instantiate a new body segment every time I hit space bar, but I can't move the body segments after they've been initiated.

I know this is a pretty basic topic and there's a lot of stuff here about moving gameobjects, but I've been searching this forum and reading answers for a while, and still can't quite figure it out. Could you guys take a look a the, and let me know what I'm doing wrong?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SnakeController : MonoBehaviour {
     
     enum moveDirection{up, down, left ,right};
     
     moveDirection d;
     
     List<Vector3> bodyPositions = new List<Vector3>();
     
     Vector3 headPosition = new Vector3(0, 0, 0);
     
     public GameObject snakeBodyPrefab;
     
     private GameObject clone;
     
     // Use this for initialization
     void Start () {
             
         
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         
         bodyPositions.Add(transform.position);
         headPosition = transform.position;
     
         
         //Control the movement
         if (Input.GetKey("up"))
             d = moveDirection.up;
         
         if (Input.GetKey("down"))
             d = moveDirection.down;
         
         if (Input.GetKey("right"))
             d = moveDirection.right;
         
         if (Input.GetKey("left"))
             d = moveDirection.left;
         
         if (d == moveDirection.up) {
             transform.position += new Vector3(0, 0, 0.1f);
         }
         
         if (d == moveDirection.down) {
             transform.position += new Vector3(0, 0, -0.1f);
         }
         
         if (d == moveDirection.right) {
             transform.position += new Vector3(0.1f, 0, 0);
             
         }
         
         if (d == moveDirection.left) {
             transform.position += new Vector3(-0.1f, 0, 0);
         }
         
         
         
         Debug.Log(headPosition.x.ToString());
         //Control growth of snake
         if (Input.GetKey(KeyCode.Space)) {
             Debug.Log("grow");
             
             clone = Instantiate(snakeBodyPrefab, transform.position, Quaternion.identity) as GameObject;
             
             clone.transform.Translate(new Vector3(0,0,0));
         }
     }
 
 }

This is the part I'm mainly having trouble with:

 clone = Instantiate(snakeBodyPrefab, transform.position, Quaternion.identity) as GameObject;
             
 clone.transform.Translate(new Vector3(0,0,0));

If I don't include the second line, it's fine, and I just keep dropping body segments where I move. But when I try to move these segments, I get "NullReferenceException: Object reference not set to an instance of an object"

Any advices would be greatly appreciated. Thank you,

Matt

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 FlyingOstriche · Sep 23, 2012 at 06:06 PM 0
Share

Those 2 lines look fine. Translating by (0,0,0) wont move the object.

Otherwise Check if snakeBodyPrefab prefab exists before instantiating. Check it goes to snakeBodyPrefab awake function. Check you havent got anything wierd in snakeBodyPrefab awake function. Check that clone exists after instantiating.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

GameObject as a constraint for generic class 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Spawning random objects. Trouble /w objects. Null Reference. 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