- Home /
Instantiate a prefab results in a new prefab in a different location than expected
Pulling my hair out, trying to use Instantiate for the first time ever... I can instantiate a copy of a prefab, but using the script below causes the prefab to be instantiated at the exact location of the "original" in scene. What I'm trying to do, is instantiate at the original start location of the original in-scene prefab. Where am I going wrong? I store the location of the original prefab at the start, but it doesn't seem to instantiate at that original location... ?
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class makenewcoin : MonoBehaviour {
public GameObject coinstartlocation ;
public Vector3 newcointransformstartlocation;
private Vector3 cointransform;
private Quaternion coinrotate;
private bool coinalreadycreated = false;
// Use this for initialization
void Start(){
Debug.Log ("location of coinstartlocation is:" + coinstartlocation.transform.position);
cointransform = coinstartlocation.transform.position;
coinrotate = coinstartlocation.transform.rotation;
}
void OnTriggerEnter(){
if (!coinalreadycreated) {
Instantiate (coinstartlocation, cointransform, coinrotate);
Debug.Log ("made a new coin at the location of gameobject: coin at" + cointransform);
coinalreadycreated = true;
}
}
void OnTriggerExit(){
coinalreadycreated = false;
}
}
Answer by SteveDizzle · Oct 22, 2017 at 09:02 PM
private Vector3 cointransform;
by default would be (0, 0, 0)
you could make it public
public Vector3 cointransform;
then enter the position you want to use in the editor
or you can try using
Instantiate (coinstartlocation, transform.position, coinrotate);
Since they set the value of cointransform in Start(), it's not clear to me why the default value is relevant.
Answer by WainWork · Oct 24, 2017 at 08:11 AM
Are u shure that problem not on parent transforms? Maybe u settings prefab location while he was a child something?