• 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
1
Question by gee4vee · Jan 18, 2018 at 05:27 AM · prefabinstantiate prefab

Prefab instantiating at wrong position

I'm following a tutorial (https://gamedevelopment.tutsplus.com/tutorials/how-to-make-an-object-shatter-into-smaller-fragments-in-unity--gamedev-11795) but when I click the space bar, the prefab gets instantiated where I originally created it, not where I've transformed it. Only difference in my scene is that I wrote in C# not JavaScript.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DestructionCtrler : MonoBehaviour {
 
     public GameObject remains; // this is the prefab, assigned manually in the editor
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKey(KeyCode.Space))
         {
             Instantiate (remains, gameObject.transform.position, gameObject.transform.rotation);
             Destroy (gameObject);
         }
     }
 }

At runtime, the prefab also instantiates as a clone (not sure if that's relevant).

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by NorthStar79 · Jan 18, 2018 at 05:54 AM

Hello, i did not watch tutorial but I am pretty sure that your code if fine. i guess your problem causes because your prefabs (or game object) have wrong transforms.

i suggest you to click your game object and reset its transform first.

how to reset transform

then do this for its every child too (then you may need to re-place them by hand)

after this, you need to reset all your prefabs too.

I sıggest you to check this tutorial please pay attention to parent-child transforms.


capture.png (10.6 kB)
Comment
Add comment · Show 1 · 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 Graithen · Oct 26, 2018 at 09:27 AM 0
Share

You sir, are a genius. I have been sat here for 30 $$anonymous$$utes trying to figure out why it was instantiating on a weird transform, but it turns out the instantiated prefab had infact inherited some bizarre transform values.

I tip my hat :) :)

avatar image
1

Answer by JVLVince · Jan 18, 2018 at 05:44 AM

As my exp, You should compare your practice and the provided source by the tutorial. You may find out where the problem is. And In my opinion, your code is no problem. I suggest you try to split the instantiate and set position process just like this:

 GameObject go = Instantiate (remains) as GameObject;
 go.transform.position = transform.position;
 go.transform.rotation = transform.rotation;

or you can try this:

 Instantiate (remains, transform.position, transform.rotation);

Maybe it's work.

Cheers!!!

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 Seraian · Mar 08, 2021 at 08:17 PM

What I realized was that an Animation in my Prefab was causing its position to get altered every time. Changing the animation to remove position properties fixed this issue.

Comment
Add comment · Show 1 · 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 gigos22 · Mar 26, 2021 at 11:36 AM 0
Share

Thank you! This is exactly the problem I had. For some reason when I made the animation for rotation only, it included some position values... You're my savior!

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

96 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

Related Questions

Instantiate is spawning too many clones 2 Answers

How to instantiate particle effect after destroying it 2 Answers

Re-instantiated prefabs desist changes in text component 1 Answer

Instantiated objects becomes child of spawn point 0 Answers

How to instantiate a empty gameobject 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