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 mitti2000 · Mar 18, 2012 at 07:37 PM · instantiatetransformprefabtranslate

Can't move instantiated prefab

Hi everybody

I'm a beginner with Unity and I'm struggeling with a problem. I'm working on a 3D Tetris and I'm working on the code, that moves the blocks 1 unit per second.

Here is my code so far (the movementscript is on the bottom):

#pragma strict

var startingPoint : GameObject; var block1 : GameObject; var block2 : GameObject; var block3 : GameObject; var block4 : GameObject; var moving : GameObject; private var randomObject : int; private var block : GameObject; static var isSet = false; private var zaehler = 1;

function Update () { if(!isSet) //Check if ther is already an active Block { //Set random block randomObject = Mathf.Round(Random.Range(1.0,4.0)); switch (randomObject) { case 1.0: block = block1; break; case 2: block = block2; break; case 3: block = block3; break; case 4: block = block4; break; }

     //Instantiate Block
     Instantiate(block, startingPoint.transform.position, startingPoint.transform.rotation);
     //setting active to true
     isSet = true;    
     }
     
 while(isSet)
     {
     if (zaehler > 1000) 
         {
         break;
         }
     //here is the moving script
     block.transform.Translate(0,Time.deltaTime,0,Space.World);
     zaehler++;
     Debug.Log(block.transform.position);
     }
 
 }</pre>

The debugger doesn't show any error. The Debug.Log shows, that the transform.position is changing but the block is not moving.

Why not? I tried different versions (as mentioned in the reference) for the Translate script but none of them worked.

The while loop is in there cause I need it later.

Thanks for your help mitti2000

Comment
Add comment · Show 2
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 Eric5h5 · Mar 18, 2012 at 07:42 PM 0
Share

The "block" variable doesn't seem to be set to anything. It's declared but never initialized. Also, http://forum.unity3d.com/threads/5105-Gravity-3d-Tetris?p=38267&viewfull=1#post38267

avatar image fafase · Mar 18, 2012 at 07:56 PM 1
Share

You could make it all so much easier, placing all your blocks in an array, also randomObject = $$anonymous$$athf.Round(Random.Range(1.0,4.0)); does not need the rounding, randomObject is an integer so it drops after the decimal. Then you don't really need the switch then, you can simply instantiate the object with the defined prefab in the array with arr[randomObject]. Also, to instantiate smg it is

var smg =Instantiate(arr[randomObject], Vector3, rotation);

Personal point of view, I would create a separate script attached to the created object ins$$anonymous$$d of moving it from this one. It could be easier to read and debug the whole thing later on.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DaveA · Mar 18, 2012 at 08:18 PM

You need to assign the result of Instantiate to a variable (which refers to the new object), then move that, something like:

 var blk = Instantiate (block, ......

and you shouldn't do a 'while' like that, look at http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html You move the block incrementally on each call to Update

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 davedx · Mar 19, 2012 at 05:40 AM

You need to store the returned game object from Instantiate and perform your transformations on that - it looks like 'block' is your prefab, so doing any world space transforms on it are meaningless.

The docs describe it well: http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

"Clones the object original and returns the clone."

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

7 People are following this question.

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

Related Questions

How do I duplicate an object to work with a shootemup 0 Answers

Accessing prefabs 1 Answer

ReplacePrefab() resets transforms for instanced prefabs? 1 Answer

Bullet not always shooting in forward direction. Code included. 2 Answers

Howto set main.camera as a parent when main.camera itself is a child of a prefab 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