Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to find the position of spawned prefab

Discussion in 'Prefabs' started by tottsy, May 1, 2024.

  1. tottsy

    tottsy

    Joined:
    Dec 8, 2023
    Posts:
    1
    I'm trying to make a simple endless runner game where the player has to jump over obstacles (just like the dinosaur chrome game). The other way to avoid those obstacles is by smashing them into pieces. In order for this to happen, the player and the spawned prefab must be positioned far enough apart when the player presses and holds their finger on the screen. So, if the distance between the player and the obstacle is greater than, let's say, some critical distance, the obstacle will break. I'm trying to calculate the distance after the player presses on screen by finding both positions, from character and obstacle and substracting them , but I can't find the position of the latter (the spawned prefab). The code where I'm calculating the distance is attached to character game object.

    Thanks in advance.

    Zajeta slika.PNG
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,020
    well, normally it spawns where you tell it to, but if you keep a reference to it, such as myThing = Instantiate (stuff here), you can ask myThing.transform.position...