• 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 MamaCatDev · Oct 08, 2012 at 07:39 AM · transformprefabposition

How can I translate my prefab on Instantiate?

I'm trying to spawn a prefab on a GUI Button click and put it in a specific position.

I can create the object but any attempts to position it after creation inside the onclick function do not work, it always stays at (0,0,0). Can someone please unlock the mystery as to why I can't re-position the prefab on creation? I've tried setting it manually, setting it via the Instantiate command, etc. Here's my code below.

 using UnityEngine;
 using System.Collections;
 
 public class StartGame : MonoBehaviour {
 
     GameObject [] tiles = new GameObject[1];
     
     float speed = 1;
     
     void OnGUI () {
         // Make a background box
         GUI.Box(new Rect(10,10,100,90), "Menu");
 
             if(GUI.Button(new Rect(20,40,80,20), "Spawn")) {
                  tiles[0] = Instantiate(Resources.Load("tilePrefab")) as GameObject;
                  //tiles[0] = Instantiate(Resources.Load("tilePrefab"), 
                  //   new Vector3(-2,0,0),
                  //   Quaternion.identity);
                  tiles[0].transform.position = new Vector3(-2,0,0);
             }

 }
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 whydoidoit · Oct 08, 2012 at 07:40 AM 0
Share

Well that should work - have you got a script attached to the prefab that moves its position?

avatar image MamaCatDev · Oct 08, 2012 at 12:15 PM 0
Share

If it helps, the prefab is a drag-and-drop import of a Blender model that I exported to an fbx. I did this because I was having trouble with obj's and the ability to change the material/texture programmatically. There's nothing attached to the prefab other than the model. The script I pasted above is attached to an empty object that resides at (0,0,0).

Interesting note is that when I print the location to console right after the move, it does indeed print the correct location. If I move into the scene editor, select the prefab, and manually set the position to (-2,0,0) the object moves to the correct location. I was worried that maybe the unit of measurement differed between the editor and the script but I don't think that's the case either. With my luck, I'll find out that the is answer is something obvious and stupid but at least I'll have an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MamaCatDev · Oct 11, 2012 at 12:06 AM

After a lot of frustration it seems that the problem was the model itself. I noticed an issue with the scaling of the model, so I went into blender and corrected it, then re-exported and suddenly it works. I changed none of the code at all.

So, until I figure out why exactly this happened, if anyone finds this question and thinks it may apply to them, make sure your scaling is normalized in Blender before you export.

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.

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

Prefab teleporting on play mode ? 0 Answers

Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer

Checking Position 1 Answer

transform getting altered by changing prefab settings 1 Answer

Enemy only chasing pre-fab's original location, not instance's 2 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