- Home /
[SOLVED ]Instantiate prefab position
Hello, I am stuck. My script was used to transport an item to the player holder location.
Since I made the player in Prefab as well as the said object, the object in question takes the starting position rather than the updated position of the player.
Can someone please help me?
public GameObject player;
GameObject holder;
private bool isStick = false;
Rigidbody2D rb;
private bool isStoping = false;
Vector2 holderPosition;
void Start()
{
rb = GetComponent<Rigidbody2D>();
holder = player.transform.Find("holder").gameObject;
}
private void Update()
{
holder = player.transform.Find("holder").gameObject;
holderPosition = new Vector2(holder.transform.position.x, holder.transform.position.y);
}
private void FixedUpdate()
{
if (isStick == true && PlayerController.grabMode == true && PlayerController.carrying == 1)
{
transform.position = holderPosition;
rb.velocity = new Vector3(0, 0, 0);
rb.velocity = Vector2.zero;
GetComponent<CapsuleCollider2D>().enabled = false;
PlayerController.isCarrying = true;
}
Answer by unity_ek98vnTRplGj8Q · Feb 12, 2021 at 04:18 PM
A public reference to your player prefab is useless here. Instead you should find your instantiated player object in your start method with something like GameObject.Find("Player") (or something more performant). There is no way to find the instance of your player object with a reference to its prefab.
If GameObject.Find is too inefficient for you, I can help brainstorm some more efficient ways to get the reference to your instantiated player object.
Answer by Bloodjoker666 · Feb 12, 2021 at 04:25 PM
Hi, @unity_ek98vnTRplGj8Q thank you!
The sentence: "There is no way to find the instance of your player object with a reference to its prefab" helped me a lot since i wasn't able to figure it out...
GameObject.Find("Player") just worked like a charm.
Your help is really appreciated, thanks again!
Hi. If you found the correct answer, don't delete this question, please mark it as Closed(from the edit button). Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613025047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiated clone GameObject spawning at wrong position. 2 Answers
Platform cannot move up 2 Answers
Dynamic rigidbody changes his positions when I move its sprite (2D game) 0 Answers
Rigidbody2d.velocity making character stop in between 1 Answer
How to get position of instantiated object before it is destroyed 2 Answers