- Home /
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.
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
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:
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.
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 :)
Hello, I have exactly the same problem. Did you find a solution?
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.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613200122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to call the animation of prefab 0 Answers
Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer
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