Search Unity

Find position in worldspace of object in list

Discussion in 'Scripting' started by bullet1520, Aug 17, 2019.

  1. bullet1520

    bullet1520

    Joined:
    Nov 20, 2017
    Posts:
    2
    I've searched Google, Reddit and beyond... I'm also quite rusty with Unity, in my defense.
    What i'm trying to do here... I have a list of objects, to be spawned. I have a list of spawners.
    I want to select a random spawner, get its position, then instantiate a prefab at it, then remove that specific spawner from the list.
    Anybody able to give me some help or hints here?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi, i'm not sure if i'm missing something. You should only need a reference to a prefab (or list of prefabs), and the list of possible spawn locations. You then spawn a new gameobject instance by instanciating the prefab at the (randomly) chosen location like so:
    Code (CSharp):
    1. Instantiate(prefab, chosenPositionVector, Quaternion.identity)
    Random.value returns a random value between 0 and 1. You can use this to access a random element of your list by multiplying it with your list size, rounding it to an integer, and using it as an indice for your list.

    Generally you can access the position of any gameobject as gameObject.transform.position.
     
  3. bullet1520

    bullet1520

    Joined:
    Nov 20, 2017
    Posts:
    2
    Not quite what I meant.
    There may have been a miscommunication of what I meant to do, but either way, I found the solution to my issue. Thanks anyway!