• 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 GregoryNeal · May 21, 2015 at 02:47 AM · instantiateprefab

Instantiate at a position is sometimes instantiating at the wrong position

Hi I've run into an issue I've never experienced before, I'm trying to instantiate a GameObject at the position of an empty child GameObject. Here is the code I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class ShootTeleporter : MonoBehaviour {
 
     private bool isShot = false;
     private GameObject currentTeleporter;
 
     public GameObject teleporter;
     public float shootSpeed;
 
     // Use this for initialization
     void Start()
     {
     
     }
     
     // Update is called once per frame
     void Update()
     {
 
         if(Input.GetMouseButtonDown(0))
         {
             if(!isShot)
             {
                 FireTeleporter();
             }
             else
             {
                 TeleportToTeleporter();
             }
         }
 
     }
 
     void FireTeleporter()
     {
         Instantiate(teleporter,transform.position,Quaternion.identity);
     }
 
     void TeleportToTeleporter()
     {
 
     }
 }

This script is attached to the GameObject whose position I want to instantiate at. Most of the time however, the object's spawn point y position is way below where it should be. Every now and then though, it will spawn at the correct position. Another thing to mention, over time, the incorrect spawn position lowers even further in the y direction. I have no idea what is going on. Here is a picture.

alt text

capture.png (137.6 kB)
Comment
Add comment · Show 7
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 barbe63 · May 21, 2015 at 03:36 AM 0
Share

Weird indeed...

3 things comes in my $$anonymous$$d:

1)check if the pivot of your instantiated object is in its center.

2)try to push a bit away your shoot point as instantiated partially or full inside a collider might add strange behaviours.

3)check for bugs in console. Sometimes a bug from another script can mess up things badly.

Also maybe a screenshot of your instantiated object inspector could be useful.

Some guy had a similar issue and it was due to a mesh collider packed with a rigidbody.
http://answers.unity3d.com/questions/625756/gameobject-instantiated-in-wrong-position.html

avatar image GregoryNeal · May 21, 2015 at 03:56 AM 0
Share

Hey thanks for the suggestions:

1) I just checked, and the pivots are for sure in the center. All it is is a primitive cube with a RigidBody2D and BoxCollider2D attached.

2) I tried that as well, with the same results.

3) Do you think the way it's set up could cause issues? From the top down, I have my player with a basic character controller script, the holster is an empty with a script attached to make it rotate to face the current mouse position. The gun is a primitive cube with a script to keep it in the same relative position to the holster. And finally the shoot point is just an empty with the script I pasted above. Could the fact that I'm using 2D physics with 3D GameObjects cause these issues?

Here is a photo of my instantiated object's inspector:

alt text

avatar image barbe63 · May 21, 2015 at 03:25 PM 0
Share

Could the fact that I'm using 2D physics with 3D GameObjects cause these issues?

$$anonymous$$aybe, I honestly don't know. Give the 3d a try.

avatar image GregoryNeal · May 22, 2015 at 01:44 AM 0
Share

Sorry about the late response, I tinkered with it and it turns out one of the scripts was the issue. I'll be honest I have no idea what the issue with the script was, but disabling it and using a workaround fixed my problem. Thanks for trying to help though :)

avatar image Alicorne · Jul 27, 2019 at 04:19 PM 0
Share

Hello, I have exactly the same problem. Did you find a solution?

avatar image GregoryNeal Alicorne · Jul 27, 2019 at 04:50 PM 0
Share

I have not. I ended up scrapping this whole project and doing something else because of this bug. Check your rigidbody settings and all the other basics. I hope you figure it out.

avatar image Alicorne GregoryNeal · Jul 28, 2019 at 01:46 AM 0
Share

Thank you, this is driving me crazy !!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nathaniel_Alcala · Apr 05, 2020 at 04:21 PM

Hi @GregoryNeal ,

We have similar problems and here is how I fixed mine:

I noticed that when I instantiated at a point not under my player it works fine.

I tried turning off my player's gravity under its rigidbody because I thought gravity was affecting it.

Turns out it was fixed so I guess you should try turning off the rigidbody under your player.

I hope this helps. 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 EpicCrownKing · Jul 28, 2019 at 03:22 AM

Make sure the prefab's Y value is set to 0. Even though it sounds simple, this messes up quite a bit of code. Hope this helps.

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 Alicorne · Jul 28, 2019 at 04:21 AM 0
Share

This y value is set to 0, it's not this. The problem comes from the child attached to my players gun (witch moves in derection of my cursor using inverse kinematics). I want to instantiate a bullet at this position everytime I click. The value of the y position of this child attached to the gun contynusly goes down but sometimes (maybe one of ten) the bullet will instantiate at the correct position. I checked the position with debug.log(gunCanon.transform.position.y) everytime I click and it give me something like this (let's say the correct position is 10): 10 - 9.85 - 9.74 - 9.62 - 9,35 - 9.1; 10 (correct position) then again it goes down : 8.85 - 8.62... I hope this is understandable, english is not my primary language. And if I look the child object in my scene it still stays at the end of the gun canon. I really don't understand why it act like this.

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

24 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

Related Questions

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Instantiate a GameObject from a Prefab without using an original to clone 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer

Networkview on prefab consumes bandwidth or safe to use? 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